Combining section definitions in a page
I have a page:
@Html.Action("Index", "Product")
@Html.Action("Index", "Vendor")
both of these contain:
@section Head {
...
}
and (of course), my layout file contains:
<head>
...
@RenderSection("Head", required:false)
</head>
the idea being that every pag开发者_如何学Ce that has something for the head (like javascript includes, css, etc.) can do it via the section. The problem is that only one definition per page seems allowed. How is this generally handled?
I would not recommend using Section within the results of RenderAction as this is generally used for View Pages and not partials.
It would be better to have the following in your hosting layout page:
@section Head {
@Html.Action("foo")
@Html.Action("bar")
}
I couldn't find a nice way to do this, so I instead added properties to my base ViewModel that would hold the information that needed to go in the <head>
, and then have a RenderPartial("Head") in my layout page. This way things are slightly more strongly-typed too, rather than just having any page put random junk in:
// Views/Shared/Head.cshtml
@model ViewModel
@foreach (var site in model.AuthorSites)
{
<link rel="me" type="text/html" href="@site" />
}
精彩评论