2013-10-30 27 views
3

我正嘗試在我的ASP.Net MVC 4項目中使用Forloop HtmlHelpers來管理Razor局部視圖的腳本。HtmlHelpers用於管理Razor局部視圖的腳本

'System.Web.Mvc.HtmlHelper' 不包含一個定義 'BeginScriptContext',沒有擴展方法 'BeginScriptContext' -

<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」的第一個參數可以發現(使用指令或程序集引用是否缺少 ?)

我已經通過包管理器控制檯安裝了此包。 如何解決這個問題?

謝謝。

回答

2

您需要

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

添加Forloop.HtmlHelpers命名空間的Web.configViews文件夾內的應用程序。

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