用于管理 Razor 局部视图脚本的 HtmlHelpers [英] HtmlHelpers for managing scripts for Razor Partial Views

查看:52
本文介绍了用于管理 Razor 局部视图脚本的 HtmlHelpers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Forloop HtmlHelpers 来管理 Razor Partial Views 的脚本在我的 ASP.Net MVC 4 项目中.

 

//这里有一些标记

@{//开始脚本的上下文Html.BeginScriptContext();Html.AddScriptBlock(@"$(function() { alert('hello from the page'); } });");Html.AddScriptFile("~/Scripts/jquery-ui-1.8.11.js");//结束脚本上下文Html.EndScriptContext();}

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

<块引用>

'System.Web.Mvc.HtmlHelper' 不包含定义'BeginScriptContext' 和无扩展方法 'BeginScriptContext'接受类型的第一个参数可以找到System.Web.Mvc.HtmlHelper"(您是否缺少using 指令或程序集引用?)

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

谢谢.

解决方案

需要添加Forloop.HtmlHelpers命名空间

 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><pages pageBaseType="BaseTemplate.Web.Extensions.BaseTemplateWebViewPage"><命名空间><!-- 将以下命名空间添加到其他命名空间 --><add namespace="Forloop.HtmlHelpers"/></命名空间></页面></system.web.webPages.razor>

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

nuget 软件包的未来更新将为您添加此功能.与此同时,我已经用这些信息更新了维基页面 :)

现在作为 nuget 包安装的一部分执行.

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

@using (Html.BeginScriptContext()){Html.AddScriptBlock(@<script type="text/javascript">$(function() { alert('来自页面的你好'); } }););Html.AddScriptFile("~/Scripts/jquery-ui-1.8.11.js");}

I'm trying to use Forloop HtmlHelpers for managing scripts for Razor Partial Views in my ASP.Net MVC 4 project.

 <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();
   }

But, it's throwing the following error during compile -

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'BeginScriptContext' and no extension method 'BeginScriptContext' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

I've installed this package via package manager console. How to overcome this issue?

Thank you.

解决方案

You need to add the Forloop.HtmlHelpers namespace

  <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>

to the Web.config within the Views folder in your application.

A future update of the nuget package will add this for you. In the meantime, I have updated the wiki page with this information :)

EDIT : This is now performed as part of the nuget package installation.

Also, I would recommend using the following syntax as it's a little more terse

@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");
}

这篇关于用于管理 Razor 局部视图脚本的 HtmlHelpers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆