Multiple select list with items already selected using Razor

Asked

Viewed 1,186 times

2

I’m using Razor to generate a SelectList thus:

@Html.DropDownList("StatusImovel", new SelectList(ViewBag.ListaStatus, "IdStatus", "Descricao"), new { @hidden = "hidden", @multiple = "multiple", @form = "nulo" })

I need it to load already with some selected items, so I’m trying this:

@Html.DropDownList("StatusImovel", new SelectList(ViewBag.ListaStatus, "IdStatus", "Descricao", ViewBag.StatusSelecionados), new { @hidden = "hidden", @multiple = "multiple", @form = "nulo" })

Whereas ViewBag.StatusSelecionados is a array int[] with the IdStatus that I want. but it’s not working.

There’s some way to do it?

1 answer

2


You are using the wrong helper, if you want multiple values to be selected, you need to use the MultiSelectList()

@Html.DropDownList("StatusImovel", new MultiSelectList(ViewBag.ListaStatus, "IdStatus", "Descricao", ViewBag.StatusSelecionados), new { @hidden = "hidden", @multiple = "multiple", @form = "nulo" })
  • Good Leandro! It worked. Thank you.

  • @Alamo then scores as the answer :)

Browser other questions tagged

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