The problem I'm running into is all the examples I can find reference .Name and .Url but I would like to access the custom properties I've set up. I'd like the search result page to contain the page title and meta description tags which I set up in my doctypes.
I realize I'm missing some core competency of Umbraco and .Net but any help would be appreciated. Here is my view...
You can access them by using result.Fields["yourPropertyAlias"]
If you're just after a basic search I would suggest installing the ezSearch package as this can give you something straight out of the box, and by checking out the code you can quickly figure out how to customise the results yourself.
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Fields' and no extension method 'Fields' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 37: <li>
Line 38: <strong><a href="@result.Url">@result.Name</a></strong> <br>
Line 39: @result.Fields["pageTitle"] Line 40: </li>
Line 41: }
Examine Search - how to access custom properties in MVC view
Hi all,
I'm not a .NET dev, sorry sorry if this is obvious. I have set up search and with little fuss it's working great. I used this quick start guide - https://our.umbraco.org/documentation/Reference/Searching/Examine/quick-start
The problem I'm running into is all the examples I can find reference .Name and .Url but I would like to access the custom properties I've set up. I'd like the search result page to contain the page title and meta description tags which I set up in my doctypes.
I realize I'm missing some core competency of Umbraco and .Net but any help would be appreciated. Here is my view...
@if (!string.IsNullOrEmpty(Request.QueryString["query"]))
{
var searchResults = Model.Search(Request.QueryString["query"]);
<h3>Found (@searchResults.Count()) result(s) for <strong>@Request.QueryString["query"]</strong> </h3>
<ul class="list-unstyled">
@foreach (var result in searchResults)
{
<li>
<strong><a href="@result.Url">@result.Name</a></strong>
</li>
}
</ul>
}
Hi Greg,
You can access them by using result.Fields["yourPropertyAlias"]
If you're just after a basic search I would suggest installing the ezSearch package as this can give you something straight out of the box, and by checking out the code you can quickly figure out how to customise the results yourself.
Cheers,
Maff
Thanks Maff!!
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Fields' and no extension method 'Fields' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 37: <li> Line 38: <strong><a href="@result.Url">@result.Name</a></strong> <br> Line 39: @result.Fields["pageTitle"] Line 40: </li> Line 41: }
Source File: c:\inetpub\rncorp\Views\Search.cshtml Line: 39
My bad Maff, I needed a conditional! Thanks again!
@if (result.HasValue("pageTitle")) {
@result.GetPropertyValue("pageTitle")
} else {
@result.Name
}
is working on a reply...