TEnum not identified in html

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 View the TEnum was identified as an html tag.

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

How do I make html interpret TipoObjetoEnum as a TEnum instead of a tag?

    
asked by anonymous 28.06.2016 / 20:10

1 answer

2

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

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

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

2. Placing the statement in Web.config from within the 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've forgotten one important thing: Razor often interprets opening and closing keys 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.

    
28.06.2016 / 21:08