Using @section inside Razor Helper

We are trying to customize sections of our layout that are necessary, but are customizable based on a separate page. At the moment, we are doing this using the 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 configure this block on every new page, but I wanted to make this process easier with some default settings and settings for setting using a partial view. I was hoping to set up 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 for implementation will be something like

@Layout.FloatingNav(@Model.Name, @Model.AverageRating)

or

@Layout.FloatingNav("_SimpleNav", @Model)

The problem is that it seems that Razor helpers do not understand the syntax of the section. Is there a way to include sections in Razor helpers?

+4
1

, .

@helper @section - .

A HelperResult () , .

DefineSection WebPageBase.

, . , , .

+3

Source: https://habr.com/ru/post/1535852/


All Articles