我在渲染部分视图的脚本标签时遇到问题,应该在 _Layout.cshtml 页面的底部渲染。
问问题
928 次
1 回答
0
我得到了解决方案,您可能不想在每个页面上呈现所有内容。切片使您可以控制它。
在_Layout .cshtml
这样定义脚本标签中,
<script src="/scripts/libs/jquery-ui-1.10.3.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
@if (IsSectionDefined("bottomScripts"))
{
@RenderSection("bottomScripts", required: false)
}
在YourView.cshtml
定义这样的部分
@section bottomScripts{
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
}
这是 MVC 中 Layout 和 Views 的标准结构。
_Layout.cshtml
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset=" utf-8">
<title>@ViewBag.Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="~/content/css/stye1.css" />
<link rel="stylesheet" href="~/content/css/style2.css" />
@if (IsSectionDefined("myStyles"))
{
@RenderSection("myStyles", required: false);
}
</head>
<body>
<header>
<a href="/Index">
<h1>My Website<small>Your Slogan</small></h1>
</a>
</header>
<div>
@RenderBody()
</div>
<script src="~/Scripts/script1.js"></script>
<script src="~/Scripts/script2.js"></script>
@if (IsSectionDefined("myScripts"))
{
@RenderSection("myScripts", required: false);
}
</body>
</html>
View1.cshtml
<div>This is View1</div>
@section myStyles{
<link rel="stylesheet" href="~/Pagespecificstyle.css" />
}
@section myScripts{
<script src="~/PageSpecificScript.js"></script>
}
View2.cshtml
<div>This is View2</div>
@section myStyles{
<link rel="stylesheet" href="~/Pagespecificstyle2.css" />
}
@section myScripts{
<script src="~/PageSpecificScript2.js"></script>
}
于 2016-04-07T06:31:03.050 回答