我有一个问题,我的部分视图中的一些脚本标签应该在_Layout.cshtml页面的底部呈现。
答案 0 :(得分: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.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>
}