Zap*_*ica 2 partial-views asp.net-mvc-4
我发现了一些与此相关的问题,但通常有许多不同的答案,它们看起来都非常混乱和复杂.
如果这是需要做的事情,那么好吧我最好坐下来解决它.
我想知道最简单和最有效的方法是从部分视图向头部添加内容.
我需要这样做的原因是我需要在每个页面上使用某些java脚本和jquery,并且它在页面之间有所不同.我不想只在_layout视图中添加它们.
Elv*_*dov 14
您可以使用部分执行此操作.例如:我有两个以上的视图,彼此具有相同的_Layout.My公司控制器中的索引操作有以下部分:
@model Invoice.Model.HelperClasses.CompanyViewModel
@{
ViewBag.Title = "Companies";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section usage{
<link href="~/css/uniform.default.css" rel="stylesheet" />
}
@section other{
<link href="~/css/datepicker.css" rel="stylesheet" />
<link href="~/css/SimpleSlide.css" rel="stylesheet" />
<link href="~/css/responsive-tables.css" rel="stylesheet" />
}
@section script
{
<script src="~/js/datepicker/bootstrap-datepicker.js"></script>
}
Run Code Online (Sandbox Code Playgroud)
Invoice控制器中的和显示操作具有相同的部分,但不同的css和js如下:
@model Invoice.Model.HelperClasses.InvoiceViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section usage{
@*<link href="~/css/uniform.default.css" rel="stylesheet" />*@
}
@section other{
<link href="~/css/DT_bootstrap.css" rel="stylesheet" />
<link href="~/css/responsive-tables.css" rel="stylesheet" />
<script src="~/js/datatables/extras/ZeroClipboard.js"></script>
}
@section script
{
<script src="~/js/datepicker/bootstrap-datepicker.js"></script>
<script src="~/js/validate/jquery.metadata.js"></script>
<script src="~/js/validate/jquery.validate.js"></script>
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在_Layout中使用这个部分,但是它的必需参数应该是false.看着:
<!DOCTYPE html>
<html>
<head>
<!--usage-->
@RenderSection("usage", required: false)
<!--other-->
@RenderSection("other", required: false)
<!--script-->
@RenderSection("script", required: false)
<head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23161 次 |
| 最近记录: |