Multiple select list with items already selected using Razor

2

I'm using Razor to generate a SelectList like this:

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

Being ViewBag.StatusSelecionados is array int[] with IdStatus I want. but it is not working.

Do you have any way to do this?

    
asked by anonymous 03.01.2018 / 13:42

1 answer

2

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

@Html.DropDownList("StatusImovel", new MultiSelectList(ViewBag.ListaStatus, "IdStatus", "Descricao", ViewBag.StatusSelecionados), new { @hidden = "hidden", @multiple = "multiple", @form = "nulo" })
    
03.01.2018 / 13:52