7

I am creating a helper with ASP NET MVC 3 and Razor to display my grid

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

but the Razor can not find @Html.ActionLink and the following error occurs

Compiler Error Message: CS1061: 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ActionLink' and no extension method 'ActionLink' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

what is wrong? ?? how to solve this problem??

Thank You

Leandro Prado

Leandro Prado
  • 73
  • 1
  • 3

1 Answers1

10

Add @using System.Web.Mvc.Html.

This is added automatically in Views\Web.config, so it won't apply to any pages outside of the Views folder.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Unfortunately, you cannot use MVC helpers in App_Code. You can still use WebPages helpers. – SLaks Feb 11 '11 at 15:50
  • OK, but how to create my helper?? – Leandro Prado Feb 11 '11 at 15:56
  • @Leandro: You can do something like this: http://stackoverflow.com/questions/4710853/using-mvc-htmlhelper-extensions-from-razor-declarative-views – SLaks Feb 11 '11 at 16:00
  • @SLaks Thank you this saved my bacon. My deploy script excluded *.config when copying the website to the server.... – edosoft Oct 07 '11 at 07:38
  • I was using a page outside the Views folder and got this error, which disappeared after adding the @using directive. I'll try and figure if I can also change web.config to fix it; just as an exercise. Bookmarked slaks blog. Thanks. – Tom Wilson Nov 06 '11 at 16:29