Asp.net mvc 4 从局部视图向视图头部添加css、js或其他内容

Asp.net mvc 4 从局部视图向视图头部添加css、js或其他内容,asp.net-mvc-4,partial-views,Asp.net Mvc 4,Partial Views,我发现了一些与此相关的问题,但通常有许多不同的答案,它们看起来都非常混乱和复杂 如果这是需要做的,那么好吧,我最好坐下来解决它 我想知道最简单、最有效的方法是从局部视图向头部添加内容 我之所以需要这样做,是因为我需要在每个页面上使用特定的java脚本和jquery,而且每页都不同。我不想将它们全部添加到布局视图中。您可以通过节来实现这一点。例如: 我有两个以上的视图,它们彼此具有相同的_布局。我在公司控制器中的索引操作有以下部分: @model Invoice.Model.HelperClass

我发现了一些与此相关的问题,但通常有许多不同的答案,它们看起来都非常混乱和复杂

如果这是需要做的,那么好吧,我最好坐下来解决它

我想知道最简单、最有效的方法是从局部视图向头部添加内容


我之所以需要这样做,是因为我需要在每个页面上使用特定的java脚本和jquery,而且每页都不同。我不想将它们全部添加到布局视图中。

您可以通过节来实现这一点。例如: 我有两个以上的视图,它们彼此具有相同的_布局。我在公司控制器中的索引操作有以下部分:

@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>
}
@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>
}
@model Invoice.model.HelperClasses.CompanyViewModel
@{
ViewBag.Title=“公司”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@节用法{
}
@其他部分{
}
@节脚本
{
}
发票控制器中的显示操作具有相同的部分,但css和js不同,如下所示:

@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>
}
@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>
}
@model Invoice.model.HelperClasses.InvoiceViewModel
@{
ViewBag.Title=“Index”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@节用法{
@**@
}
@其他部分{
}
@节脚本
{
}
然后,您可以在_布局中使用此部分,但其必需参数应为false。看看:

<!DOCTYPE html>
<html>
<head>
<!--usage-->
  @RenderSection("usage", required: false)
<!--other-->
  @RenderSection("other", required: false)
<!--script-->
  @RenderSection("script", required: false)
<head>
<body>
</body>
</html>

@RenderSection(“用法”,必填项:false)
@RenderSection(“其他”,必填项:false)
@RenderSection(“脚本”,必需:false)

在您的_Layout.cshtml页面(或任何其他母版页)上,在
标记内部使用以下代码

@if (IsSectionDefined("SpecialOther"))
{
    @RenderSection("SpecialOther")
}
在需要特殊css、脚本或任何其他项目的页面上,指定它们的引用。e、 g

    @section SpecialOther{
        <link href="~/css/responsive-tables.css" rel="stylesheet" />
  <script src="~/js/datatables/extras/ZeroClipboard.js"></script>
    }
@section speciallother{
}

感谢您提供此解决方案,我将尝试一下,您能否向我解释一下“@”部分用法与“@”部分用法的区别?欢迎使用。如果对您有效,您能否将答案标记为已接受。:)。用法是您将在每个视图中使用它的节,但不是必需的,其他节-它属于视图。因此,名称“usage”和“other”是正式名称,您可以将其视为表示特定css和js链接的变量。