Last active
August 29, 2015 14:17
-
-
Save dampee/c49c7f972e3d86da9169 to your computer and use it in GitHub Desktop.
Simple full text search in Umbraco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@* The template in /Views folder *@ | |
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Models.SearchModel> | |
@{ | |
Layout = "Master.cshtml"; | |
} | |
<h1>U zocht naar: "@Model.QueryString"</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@* 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"> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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