How to do an ASP NET MVC dropdown

6

I have a DTO Class

[Serializable]
    public class PerfilDTO
    {
        public int Codigo { get; set; }
        public string Descricao { get; set; }
        public SituacaoEnum Situacao { get; set; }
        public List<PerfilFuncionalidadeDTO> PerfilFuncionalidade { get; set; }

    }

And in this DTO I have my SituacaoEnum which is the one below:

 public enum SituacaoEnum
    {
        [Description("Ativo")]
        Ativo = 1,
        [Description("Inativo")]
        Inativo = 2
    }

In my view I have the following:

@model IEnumerable<ControleAcesso.PerfilDTO>

an html any e:

 @Html.DropDownListFor(this.Model.First().Situacao)

How to make a dropDown from what I have?

    
asked by okevinlira 15.05.2014 в 21:53

3 answers

2
public static class DropDownListHelper
    {
        public static List<SelectListItem> DropDownListEnum<T>(this HtmlHelper helper)
        {
            List<SelectListItem> listaItens = new List<SelectListItem>();
            SelectListItem itemVazio = new SelectListItem();
            itemVazio.Text = "Selecione uma opção";
            itemVazio.Value = "";
            listaItens.Add(itemVazio);

            SelectListItem itemLista;
            //int value = 1;
            //string description = Enumerations.GetEnumDescription((MyEnum)value);
            foreach (Enum item in Enum.GetValues(typeof(T)))
            {
                itemLista = new SelectListItem { Value = item.ToString(), Text = GetDescription(item) };
                listaItens.Add(itemLista);
            }

            return listaItens;
        }

        public static string GetDescription(Enum en)
        {
            Type type = en.GetType();

            MemberInfo[] memInfo = type.GetMember(en.ToString());

            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                return attrs != null && attrs.Length > 0 ? ((DescriptionAttribute) attrs[0]).Description : en.ToString();
            }

            return en.ToString();
        }
    }

::: EDIT :::

I found a better solution!

    
15.05.2014 / 22:04
4
@{  var lstOpcoes = Enum.GetValues(typeof(SituacaoEnum)).OfType<SituacaoEnum>().Select(sa => 
    new SelectListItem 
        {
            Text = sa.ToString(), 
            Value = ((int)sa).ToString()
        }).ToList();
}
@Html.DropDownListFor(dtos => dtos.First().Situacao, lstOpcoes)
    
15.05.2014 в 22:26
2

Hello, @okevinlira

Complementing the response to @Gigano, you will notice that you often need to repeat that code snippet where you create a list of SelectListItem , so that the options in dropdownlist are created with the values contained in the enum .

I suggest that you do a extension method for this as shown below:

public static class DropDownListHelper
{
    public static List<SelectListItem> DropDownListEnum<T>(this HtmlHelper helper)
    {
        List<SelectListItem> listaItens = new List<SelectListItem>();
        SelectListItem itemVazio = new SelectListItem();
        itemVazio.Text = "Selecione uma opção";
        itemVazio.Value = "";
        listaItens.Add(itemVazio);

        SelectListItem itemLista;

        foreach (Enum item in Enum.GetValues(typeof(T)))
        {
            itemLista = new SelectListItem();

            itemLista.Value = item.ToString();
            itemLista.Text = item.Descricao();

            listaItens.Add(itemLista);
        }

        return listaItens;
    }
}

After that, your DropDownListFor should be set up as follows:

@Html.DropDownListFor(dtos => dtos.First().Situacao, Html.DropDownListEnum<SituacaoEnum>())

In addition to allowing you to use with any other enum !

Did you find it simpler?

    
16.05.2014 в 22:28