我正在尝试使用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'的第一个参数(您是否缺少using指令或装配参考?)
我已经通过程序包管理器控制台安装了该程序包。如何克服这个问题?
谢谢你。
您需要添加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>
在Web.config
该范围内Views
的应用程序文件夹中。
nuget软件包的将来更新将为您添加。同时, 我已经用以下信息更新了Wiki页面 :)
编辑:这现在作为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");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句