将@helper代码转移到App_Code文件夹抛出错误

Raj*_*wal 4 razor asp.net-mvc-3

我有一个@heper pagination功能.那是两个View助手ViewBagUrl.这个分页将被很多页面使用,所以我将代码从Views文件App_Code夹转移到文件夹.代码里面App_Code/Helper.cshtml

@helper buildLinks(int start, int end, string innerContent)
{
     for (int i = start; i <= end; i++)
     {   
         <a class="@(i == ViewBag.CurrentPage ? "current" : "")" href="@Url.Action("index", "country", new { page = i })">@(innerContent ?? i.ToString())</a>
     }   
}
Run Code Online (Sandbox Code Playgroud)

但现在我运行应用程序.它会引发错误

error CS0103:The name 'ViewBag' does not exist in the current context
error CS0103:The name 'Url' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)

我是否需要导入任何命名空间或问题所在?

我想做的方式是完美的吗?

aka*_*key 13

实际上你可以从App_Code文件夹中的助手访问ViewBag,如​​下所示:

@helper buildLinks()
{
    var p = (System.Web.Mvc.WebViewPage)PageContext.Page;

    var vb = p.ViewBag;

    /* vb is your ViewBag */
}
Run Code Online (Sandbox Code Playgroud)