首页 > 解决方案 > asp.net _Layout.cshtml 仅在模型类型为 ______ 时加载脚本

问题描述

如果模型属于某种类型,如何仅在我的 _Layout.cshtml 中加载脚本?如果模型是我制作的自定义模型类型,我只希望我的布局加载脚本标签。

标签: javascriptc#razorasp.net-mvc-5

解决方案


你可以在你的视图中通过剃刀语法来做到这一点

   @if(Model is <yourtypehere>)
    {
          <script>alert('this is my type')</script>
          //or you can even
          <script src="myjavascriptfilelocation"/>    
    }

推荐阅读