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.
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.
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" })