Dropdownlist with data from an Enum

Asked

Viewed 677 times

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?

  • 1
  • 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.

  • 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? I think the answer to that question answers your question. Just don’t use the .Where and ready.

  • Dear, analyze the question code. They are different proposals... I do not want to assemble the SelectListItem in View with data from Enum I want to pass the TEnum for my Helper class and her being responsible for returning me to Htmlstring from the dropdown.

  • 1

    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();?

  • I added the missing methods to the question.

  • I wanted to understand the purpose of this. Why do you make a point of mounting the Dropdown within a Extension?

  • 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

  • Missed to say what the problem is. I tested your code and it’s working normally? What do you really want?

Show 4 more comments
No answers

Browser other questions tagged

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