我发现了一些与此相关的问题,但通常有很多不同的答案,而且它们看起来都非常混乱和复杂。
如果这就是需要做的,那么好吧,我最好坐下来解决它。
我想知道最简单、最有效的方法是将内容从部分视图添加到您的头脑中。
我需要这样做的原因是我需要在每个页面上使用某些java脚本和jquery,并且每个页面都不同。我不想只是想将它们全部添加到 _layout 视图中。
您可以通过部分来完成此操作。例如:
我有两个以上的视图,彼此具有相同的 _Layout。公司控制器中的我的索引操作有以下部分:
@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>
}
发票控制器中的显示操作具有相同的部分,但 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>
}
然后您可以在 _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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)