Unidentified tenum in html

Asked

Viewed 33 times

1

I created the following helper:

public static HtmlString DropDownListEnum<TEnum>(this HtmlHelper htmlHelper, string name, int? valorSelecionado = null)
    {
        List<SelectListItem> lstEnum = AttributesHelper.ToSelectList<TEnum>(valorSelecionado);

        MvcHtmlString html = htmlHelper.DropDownList(name, lstEnum, new object { });

        return html;
    }

And when trying to call it in the View Tenum was identified as an html tag.

@Html.DropDownListEnum<TipoObjetoEnum>("ddlTeste", Model.IdTipoObjetoFK)

exemplo

How I make html to interpret TipoObjetoEnum as a Tenum instead of a tag?

  • See if this helps you: http://stackoverflow.com/a/5285842/221800

1 answer

2


In fact your Helper was not recognized by View. There are two ways to solve:

1. Making @using in the statement of View of namespace where the Helper was implemented

@model SeuProjeto.Models.SeuModel
@using SeuProjeto.Helpers

2. Placing the declaration in Web.config from within the directory Views

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="SeuProjeto" />
        <add namespace="SeuProjeto.Helpers" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

EDIT

I forgot something important: Razor usually interprets key opening and closing signals as HTML. For him, it’s like you’re writing, for example:

@Html.DropDownListEnum<div></div>("ddlTeste", Model.IdTipoObjetoFK)

The way to solve it is:

@(Html.DropDownListEnum<TipoObjetoEnum>("ddlTeste", Model.IdTipoObjetoFK))

Yes, horrible, I know.

  • Both are in the same namespace, so much so that in Ctrl + Space the method appears for auto complete.

  • @Jonathanbarcela I implemented your solution here and increased my response. See if it serves you.

  • 1

    Solved my problem. Thank you :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.