加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 将@helper代码转移到App_Code文件夹抛出错误

发布时间:2020-12-16 07:37:00 所属栏目:asp.Net 来源:网络整理
导读:我有一个@heper分页功能.那就是有两个View帮助器ViewBag和Url. 这个分页将被很多页面使用,所以我从Views文件夹中移动代码 到App_Code文件夹. App_Code / Helper.cshtml中的代码 @helper buildLinks(int start,int end,string innerContent){ for (int i = st
我有一个@heper分页功能.那就是有两个View帮助器ViewBag和Url.
这个分页将被很多页面使用,所以我从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>
     }   
}

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

error CS0103:The name 'ViewBag' does not exist in the current context
error CS0103:The name 'Url' does not exist in the current context

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

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

解决方法

正如Mark所说,你应该将UrlHelper作为参数传递给你的助手:

@helper buildLinks(int start,int currentPage,string innerContent,System.Web.Mvc.UrlHelper url)
{
     for (int i = start; i <= end; i++)
     {   
         <a class="@(i == currentPage ? "current" : "")" href="@url.Action("index",new { page = i })">@(innerContent ?? i.ToString())</a>
     }   
}

然后像这样称呼它:

@Helper.buildLinks(1,10,ViewBag.CurrentPage,"some text",Url)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读