How to mount a list in Controller and mount a DropDownlist

1

I have the following difficulty.

My address class has TipoEndereco , wanted to mount a dropdownlist

Do I set up a list in the controller? Can I get her in the view?

Thank you.

    
asked by anonymous 10.12.2015 / 16:38

1 answer

1

With String looks like this:

@Html.DropDownListFor(model => model.TipoEndereco, new List<String> { "Residencial", "Comercial" }.Select(option => new SelectListItem
        {
            Text = option,
            Value = option,
            Selected = (Model != null) && (Model.TipoEndereco == option)
        }), "Selecione...", new { @class = "form-control" })

Or you do the performative way and create an Enum:

public enum TipoEndereco
{
    Residencial,
    Comercial
}

And use it like this:

@Html.DropDownListFor(model => model.TipoEndereco, Enum.GetValues(typeof(TipoTelefone)).OfType<TipoEndereco>().Select(option => new SelectListItem
        {
            Text = option.ToString(),
            Value = option.ToString(),
            Selected = (Model != null) && (Model.TipoEndereco == option)
        }), "Selecione...", new { @class = "form-control" })
    
10.12.2015 / 16:54