We are trying to setup Sections of our layout to be Required but configurable based on the individual page. At the moment we do this with a Section.
@section FloatingNav {
<h1>@Model.Name <span class="release-year">@Model.AverageRating</span></h1>
<ul class="sub-nav">
<li class="active"><a href="#episodes">Episodes</a></li>
<li><a href="#episodes">Cast</a></li>
<li>Reviews</li>
<li>Related</li>
</ul>
}
This requires you to setup this block in every new page but i wanted to make this process easier with some defaults and options to configure using a partial view. I was hoping to setup a Razor helper such as this.
@using System.Web.Mvc.Html
@helper FloatingNav(string name, int rating) {
@section FloatingNav {
<h1>
name <span class="release-year">rating</span></h1>
<ul class="sub-nav">
<li class="active"><a href="#episodes">Episodes</a></li>
<li><a href="#episodes">Cast</a></li>
<li>Reviews</li>
<li>Related</li>
</ul>
}
}
@helper FloatingNav(System.Web.Mvc.HtmlHelper html, string viewName) {
@section FloatingNav {
@html.Partial(viewName)
}
}
@helper FloatingNav(System.Web.Mvc.HtmlHelper html, string viewName, object model) {
@section FloatingNav {
@html.Partial(viewName, model)
}
}
So the syntax to implement would be something like
@Layout.FloatingNav(@Model.Name, @Model.AverageRating)
or
@Layout.FloatingNav("_SimpleNav", @Model)
The issue though is that it seems the Razor Helpers do not understand the section syntax. Is there a way to include sections in Razor Helpers?
I don't think this is possible.
The @helper
and @section
syntax are special directives for compiling pages.
A HelperResult
(a helper) doesn't know how to define a section.
The DefineSection method belongs to a WebPageBase
.
You might have to come at this from a different direction. Using partial views instead of helpers would probably fix this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With