RenderSection脚本未显示在Head Tag中

时间:2012-12-17 18:46:15

标签: asp.net-mvc asp.net-mvc-4 razor

我遇到以下问题。我有一个包含网站主要布局的母版页。然后我有一个特定的视图,它使用母版页作为布局,但添加了如下所示的其他脚本:

_Masterpage.cshtml

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        @RenderSection("head", required: false)
    </head>
    <body>
        <header> ....

_Register.cshtml

@{
    Layout = "_Master.cshtml";
}

@section head
{
    @Scripts.Render("~/bundles/jqueryval");
    @Styles.Render("~/Content/themes/base/css");
}

<div id="body">
    <div data-role="page">
        @RenderBody()
    </div>
</div>

现在我的包含表单数据的视图使用_Register.cshtml,但是当RegisterController中的Index返回视图(如下所示)时,在头部的任何地方都找不到_Register.cshtml中的脚本和样式标签

public ActionResult Index()
{
    return View();
}

有人能帮帮我吗?为了在head标签中获取这些脚本/样式,我缺少什么?

由于

1 个答案:

答案 0 :(得分:1)

节在部分视图中不起作用,这是设计的。您可以使用一些自定义帮助程序 - Using sections in Editor/Display templates - 来实现类似的行为,但老实说,视图的责任是包含必要的脚本,而不是部分责任。我建议你使用主视图的@scripts部分来做这件事而不要让部分人担心脚本

相关问题