I have a problem with the following. I have a masterpage which contains the main layout of the site. Then I have a specific view which uses the masterpage as a layout but adds additional scripts like the following:
我有以下问题。我有一个主页,包含了网站的主要布局。然后,我有一个特定的视图,它使用母版作为布局,但添加了以下脚本:
_Masterpage.cshtml
_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
_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>
Now my view which contains the form data makes use of the _Register.cshtml, however when the Index in the RegisterController returns the view (like below), the scripts and styles in the _Register.cshtml are not found anywhere in the Head tag.
现在,包含表单数据的视图使用了_Register。然而,当RegisterController中的索引返回视图(如下所示)时,_Register中的脚本和样式。cshtml在头标记的任何地方都找不到。
public ActionResult Index()
{
return View();
}
Can anyone help me please? What am I missing in order to get these scripts/styles in the head tag please?
谁能帮帮我吗?为了在head标签中找到这些脚本/样式,我缺少了什么?
Thanks
谢谢
1 个解决方案
#1
1
Sections don't work in partial views and that's by design. You may use some custom helpers - Using sections in Editor/Display templates - to achieve similar behavior, but honestly it's the view's responsibility to include the necessary scripts, not the partial's responsibility. I would recommend you using the @scripts section of the main view to do that and not have the partials worry about scripts
部分视图不工作,这是设计的结果。您可以使用一些自定义帮助器(使用编辑器/显示模板中的节)来实现类似的行为,但是老实说,视图的责任是包含必要的脚本,而不是部分的责任。我建议您使用main视图的@scripts部分来实现这一点,而不要让部分担心脚本
#1
1
Sections don't work in partial views and that's by design. You may use some custom helpers - Using sections in Editor/Display templates - to achieve similar behavior, but honestly it's the view's responsibility to include the necessary scripts, not the partial's responsibility. I would recommend you using the @scripts section of the main view to do that and not have the partials worry about scripts
部分视图不工作,这是设计的结果。您可以使用一些自定义帮助器(使用编辑器/显示模板中的节)来实现类似的行为,但是老实说,视图的责任是包含必要的脚本,而不是部分的责任。我建议您使用main视图的@scripts部分来实现这一点,而不要让部分担心脚本