11

ASP.NET MVC3プロジェクトには、_Layout.cshtmlVisual 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 ScriptContentIndex.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は実行されません。原因は何でしょうか?ありがとう

4

1 に答える 1

16

@section部分ビューからレイアウトにを設定することはできません。回避策として、代わりに必要な<script>HTMLをレンダリングするアクションを呼び出すことができますが、これはあまりエレガントではありません。

于 2012-12-19T13:23:18.100 に答える