Best Approach: Mutliple content place holders in MVC
I am converting an ASP.Net web forms project to MVC3. The master page contains multiple content place holders at different locations.
I replaced the first content place holder with @RenderBody()
b开发者_Python百科ut I am confused with what to do of the second one.
One approach might be to separate views and place a @Html.RenderAction()
for each content place holder method. Is there a better way to do it?
Razor has got sections
understanding in place of asp.net webforms ContentPlaceHolders. Take a look at this introductionary link.
You can use sections. For example, to have a section for scripts, in the head tag of the layout.cshtml, you can specifiy
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
@RenderSection("scripts", false);
</head>
Inside of any view, you can now add a scripts section to inject your scripts:
@section scripts{
<script src="@Url.Content("~/Scripts/myscript.js")" type="text/javascript"></script>
}
the "false" param tells MVC to render the section if exists on the child page or do nothing if no call
精彩评论