Skip to content

Instantly share code, notes, and snippets.

@dampee
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dampee/c49c7f972e3d86da9169 to your computer and use it in GitHub Desktop.
Save dampee/c49c7f972e3d86da9169 to your computer and use it in GitHub Desktop.
Simple full text search in Umbraco
@* The template in /Views folder *@
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Models.SearchModel>
@{
Layout = "Master.cshtml";
}
<h1>U zocht naar: &quot;@Model.QueryString&quot;</h1>
<ul>
@foreach (var item in Model.Results)
{
<li>
<a href="@item.Url">
<strong> @item.Name</strong>
@if (item.HasProperty("bodyText"))
{
@(Umbraco.Truncate(Umbraco.StripHtml(item.GetPropertyValue<string>("bodyText")), 150, true))
}
</a>
</li>
}
</ul>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
namespace Controllers
{
/// <summary>
/// Summary description for SearchController
/// </summary>
public class HomeController : RenderMvcController
{
public ActionResult Search(RenderModel model)
{
var queryString = Request.QueryString["q"];
var searchModel = new Models.SearchModel(model);
searchModel.QueryString = queryString;
searchModel.Results = Umbraco.TypedSearch(queryString);
return CurrentTemplate(searchModel);
//return View(searchModel);
}
}
}
@* A partial view in /Views/Partials/ folder *@
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using (Html.BeginUmbracoForm<Controllers.SearchFormController>("Perform", FormMethod.Get))
{
<input type="search" placeholder="Vul een zoekwoord in en druk op enter" class="form-control" name="q">
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
namespace Controllers
{
/// <summary>
/// Summary description for SearchController
/// </summary>
public class SearchFormController : SurfaceController
{
public ActionResult Perform(string q)
{
return Redirect("/search?q=" + q);
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Umbraco.Core.Models;
using Umbraco.Web.Models;
namespace Models
{
public class SearchModel : RenderModel
{
public SearchModel(RenderModel model)
: this(model.Content, model.CurrentCulture)
{ }
public SearchModel(IPublishedContent content, CultureInfo culture)
: base(content, culture)
{ }
public SearchModel(IPublishedContent content)
: base(content)
{ }
public string QueryString { get; set; }
public IEnumerable<Umbraco.Core.Models.IPublishedContent> Results { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment