Asp.net-Mvc

RenderSection 在 ASP.NET MVC3 的局部視圖中不起作用

  • March 20, 2017

在我的 ASP.NET MVC3 項目中,我有一個_Layout.cshtml由 Visual Studio 2010 生成的標準,在關閉我的<body>標籤後,我放置了一個RenderSection

_Layout.cshtml:

</body>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
@RenderSection("ScriptContent", required: false)
</html>

然後在我看來,Index.cshtml我有:

@model MyApp.ViewModels.MyViewModel
@{ Html.RenderPartial("MyPartial", Model);  }

如果我將@section ScriptContent它放在 Index.cshtml 中,它會正確顯示。如果我把它放在我的部分視圖中MyPartial.cshtml

@model MyApp.ViewModels.MyViewModel

@section ScriptContent {
    <script src="@Url.Content("~/Scripts/Filters.js")" type="text/javascript"></script>    
} 

在我的頁面源中,我有:

</body>
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>    
</html>

表示@section未執行。可能是什麼原因?謝謝

不能@section從局部視圖中設置佈局。作為一種解決方法,您可以改為呼叫呈現必要<script>和 HTML 的操作 - 儘管這不是很優雅。

引用自:https://stackoverflow.com/questions/13953258