【问题标题】:Create custom Helper in ASP NET MVC 3 and Razor在 ASP NET MVC 3 和 Razor 中创建自定义 Helper
【发布时间】:2011-06-25 13:53:10
【问题描述】:

我正在使用 ASP NET MVC 3 和 Razor 创建一个助手来显示我的网格

@helper ListaPessoa(IEnumerable<TesteHelpersMV3.Models.PessoaModel> listaPessoa) 
{    
    <table>
    <tr>
        <th></th>
        <th>Nome</th>
        <th>Endereco</th>
        <th>DataNascimento</th>
    </tr>

    @foreach (var item in listaPessoa)
    {
        <tr>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Nome }) |
                @Html.ActionLink("Details", "Details", new { id = item.Nome }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Nome })
            </td>
            <td>@item.Nome</td>
            <td>@item.Endereco</td>
            <td>@item.Cidade</td>
        </tr>
    }

    </table>
}

但 Razor 找不到 @Html.ActionLink 并出现以下错误

编译器错误消息:CS1061:“System.Web.WebPages.Html.HtmlHelper”不包含“ActionLink”的定义,并且没有扩展方法“ActionLink”接受“System.Web.WebPages.Html”类型的第一个参数.HtmlHelper' 可以找到(您是否缺少 using 指令或程序集引用?)

怎么了? ??这个问题怎么解决??

谢谢

莱安德罗·普拉多

【问题讨论】:

    标签: asp.net-mvc razor


    【解决方案1】:

    添加@using System.Web.Mvc.Html

    这是 Views\Web.config 中的 added automatically,因此它不适用于 Views 文件夹之外的任何页面。

    【讨论】:

    • 很遗憾,您不能在 App_Code 中使用 MVC 助手。您仍然可以使用网页助手。
    • 好的,但是如何创建我的助手??
    • @Leandro:你可以这样做:stackoverflow.com/questions/4710853/…
    • @SLaks 谢谢你救了我的培根。将网站复制到服务器时,我的部署脚本排除了 *.config....
    • 我在使用 Views 文件夹外的页面时遇到此错误,在添加 @using 指令后该错误消失了。我会尝试弄清楚是否也可以更改 web.config 来修复它;就像一个练习。收藏 slaks 博客。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多