【问题标题】:HtmlHelpers for managing scripts for Razor Partial Views用于管理 Razor 部分视图的脚本的 HtmlHelpers
【发布时间】:2017-02-24 21:25:28
【问题描述】:

我正在尝试使用 Forloop HtmlHelpers 在我的 ASP.Net MVC 4 项目中管理 Razor 局部视图的脚本。

 <div class="row-fluid">
  //some markups here
 </div>

 @{
  // begin a context for the scripts
   Html.BeginScriptContext();

   Html.AddScriptBlock(@"$(function() { alert('hello from the page'); } });");
   Html.AddScriptFile("~/Scripts/jquery-ui-1.8.11.js");

   // end the script context
   Html.EndScriptContext();
   }

但是,它在编译过程中抛出以下错误 -

'System.Web.Mvc.HtmlHelper' 不包含对 'BeginScriptContext' 并且没有扩展方法 'BeginScriptContext' 接受类型的第一个参数 可以找到“System.Web.Mvc.HtmlHelper”(您是否缺少 使用指令还是程序集引用?)

我已经通过包管理器控制台安装了这个包。 如何克服这个问题?

谢谢。

【问题讨论】:

    标签: asp.net-mvc-4 partial-views


    【解决方案1】:

    你需要添加Forloop.HtmlHelpers命名空间

      <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="BaseTemplate.Web.Extensions.BaseTemplateWebViewPage">
          <namespaces>
            <! -- Add the following namespace to the others -->
            <add namespace="Forloop.HtmlHelpers" />
          </namespaces>
        </pages>
      </system.web.webPages.razor>
    

    到您应用程序中Views 文件夹中的Web.config

    nuget 包的未来更新将为您添加此内容。与此同时,I have updated the wiki page with this information :)

    编辑:现在作为 nuget 包安装的一部分执行。

    另外,我建议使用以下语法,因为它更简洁

    @using (Html.BeginScriptContext())
    {
        Html.AddScriptBlock(
           @<script type="text/javascript">
               $(function() { alert('hello from the page'); } });
           </script>);
        Html.AddScriptFile("~/Scripts/jquery-ui-1.8.11.js");
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 1970-01-01
      • 2011-09-01
      • 2012-03-12
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多