0
I need to make a Helper where I pass an Enum and it mounts a Dropdownlist based on all values of Enum, I tried to do as follows:
C#:
public static HtmlString DropDownListEnumFor(this HtmlHelper htmlHelper, Type _enum, string name, int value)
{
List<SelectListItem> lst = new List<SelectListItem>();
lst.Add(new SelectListItem()
{
Text = string.Empty,
Value = string.Empty
});
//percorre objetos do enum
foreach (var item in (_enum).GetFields())
{
if (item.FieldType == (_enum))
{
//busca atributos definidos
var e = item.GetValue(item);
var nome = AttributesHelper.DescricaoEnum(e);
//carrega list de retorno
if (!string.IsNullOrEmpty(nome))
{
lst.Add(new SelectListItem()
{
Text = nome,
Value = ((int)item.GetValue(item)).ToString(),
Selected = value == e.GetHashCode()
});
}
}
}
MvcHtmlString html = htmlHelper.DropDownList(name, lst, new object { });
return html;
}
public static T Convert<T>(object value)
{
var output = value.ToString();
var member = value.GetType().GetMember(output).First();
var attributes = member.GetCustomAttributes(typeof(T), false);
T obj = attributes.Cast<T>().FirstOrDefault();
return obj;
}
public static string DescricaoEnum(object obj)
{
return Convert<DescriptionAttribute>(obj).Description;
}
Html:
@Html.DropDownListEnumFor(typeof(TipoObjetoEnum), "ddlTeste", 1)
What would be the best way for me to pass a kind of Enum to my class and get to do what was mentioned?
Possible duplicate of Choose which items of a enumerator appear in an Enumdropdownlistfor
– Randrade
They are completely different questions... They are only because it deals with the same subject that is considered duplicate. I know how to assemble the Dropdownlist based on the Enum data, what I want is to pass the Enum type to an external method to my View and with that take the Enum data.
– Jonathan Barcela
What would be the best way for me to pass a kind of Enum to my class and get to do what was mentioned? I think the answer to that question answers your question. Just don’t use the
.Where
and ready.– Randrade
Dear, analyze the question code. They are different proposals... I do not want to assemble the
SelectListItem
in View with data fromEnum
I want to pass theTEnum
for my Helper class and her being responsible for returning me to Htmlstring from the dropdown.– Jonathan Barcela
This was not clear in the question. When you asked "the best way", I thought you did not want to fix what you already have, but "a better way". Well, let’s solve your problem then. What’s the problem the way it is? Could post the
AttributesHelper.DescricaoEnum();
?– Randrade
I added the missing methods to the question.
– Jonathan Barcela
I wanted to understand the purpose of this. Why do you make a point of mounting the Dropdown within a Extension?
– Leonel Sanches da Silva
The fact that my application has the use of Enuns in Dropdowns in several places made me think about this type of solution, so I don’t have to replicate code in all Controllers, where I always have to create a Viewbag to pass the Selectlist to View. so I thought it would be better to create a Helper
– Jonathan Barcela
Missed to say what the problem is. I tested your code and it’s working normally? What do you really want?
– Randrade