I have just tested the code I posted earlier and Icansee that is notpossible yet to translate theRazor that comes fromfromquerybuilder,to stronglytyped
I was doing it like Dennis' second example but then when I set up child doctypes on pages with the menu it started throwing an error. I couldn't figure out why but using the "Visible" dynamic query feature instead of linq fixed it for some reason. May have been some localised issue to my code but if you get it try doing like this:
var selection =Model.Content.AncestorOrSelf(1).Children.Where("Visible");
Strongly typed Menu
Hi. Umbraco has a nice menu snippet like this:
@{ var selection = CurrentPage.Site().Children.Where("Visible"); }
<ul>
@foreach (var item in selection)
{
<li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
Does anyone knows what this would be if I want it strongly typed, ie that I can use the intelegenser in Visual Studio to edit the code.
/Lars
Hi Lars,
I think that you can do it like this.
Hope this helps,
/Dennis
Hello,
You could also build your menu in a controller and render that. That way it's strongly typed and no linq queries in your views. For example:
Menu logic
https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/BLL/ModelLogic.cs#L44
Menu rendering:
https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Site/Views/Master.cshtml#L32
Have a look at the Hybrid Framework.
Jeroen
Hi Lars,
I have just tested the code I posted earlier and I can see that is not possible yet to translate the Razor that comes fromfrom query builder, to strongly typed
But this should do the same,
@{var selection = Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible());}
<ul>
@foreach(var item in selection)
{
<li class="@(item.IsAncestorOrSelf(Model.Content) ? "current" : null)">
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
Hope this helps,
/Dennis
I was doing it like Dennis' second example but then when I set up child doctypes on pages with the menu it started throwing an error. I couldn't figure out why but using the "Visible" dynamic query feature instead of linq fixed it for some reason. May have been some localised issue to my code but if you get it try doing like this:
var selection =Model.Content.AncestorOrSelf(1).Children.Where("Visible");
Hi Both
This was the solution. Now it is strongly typed. Thank you for your help :-)
/Lars
@{ IPublishedContent root = Model.Content.AncestorOrSelf(1); }
<ul>
<li class="@((Model.Content.Name == root.Name) ? "active" : null)"><a href="/">@root.Name</a></li>
@foreach (IPublishedContent menuItem in root.Children.Where(x => x.IsVisible()))
{
else
{
<li class="@((menuItem.Name == Model.Content.Name) ? "active" : null)">
<a href="@menuItem.Url">@menuItem.Name</a>
</li>
}
}
}
</ul>
is working on a reply...