How to build a list in Controller and mount a Dropdownlist

Asked

Viewed 72 times

1

I have the following difficulty.

My class addresses have the TipoEndereco, wanted to mount a dropdownlist

Shall I set up a list on the controller? I got her in the view?

Thank you.

  • What do you have? This question can generate many results. Be more specific so we can help you.

  • 1

    http://stackoverflow.com/questions/22346376/how-to-displaying-a-list-of-objects-in-mvc-view

  • TipoEndereco is an Enum?

  • No, string even @Ciganomorrisonmendez

  • Take a look at the link I sent @Furlan

1 answer

1


With String gets 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 it performatively and create an Enum:

public enum TipoEndereco
{
    Residencial,
    Comercial
}

And uses 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" })

Browser other questions tagged

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