DropDownListFor selected values do not persist

2

I'm using ASP.NET C # with mvc 4 and in my View I have a DropDownListFor, with a class I used from bootstrap-multiselect, as in the image:

Andthecodeintheview:

@Html.DropDownListFor(m => m.Estagios, (IEnumerable<SelectListItem>)Model.Estagios, new { @class = "multiselect form-control", multiple = "multiple" })

When I do the search, I make a Post in a method on the Controller and inside this method (both get, and post) feed the ViewModel and SelectList:

Sohe'spickingupthevaluesIposted,whichare1,3,and5.IgiveaReturnViewbypassingtheViewModel.Whenitpostsandthepagereloads,itassumesthevalueintherightquerystringanddoesallthecorrectsearchhandlingasprogrammed,howeveritdoesnotassumethepickervaluesintheDropDownList,bringingonlythefirstonefromthelist:

Am I doing something wrong?

The plugin used by css / javascript is this: davidstutz.github.io/bootstrap-multiselect

    
asked by anonymous 02.08.2017 / 17:43

2 answers

1

I do not know if this is how MultiSelect works.

So, your HTML will be a ListBox and not a DropDownListFot

 @Html.ListBox("Estagios", null, new { @class = "multiselect-group-clickable" })

Your script will be

   <script language="javascript">
$(document).ready(function () {

    $('#Estagios').multiselect({
        includeSelectAllOption: true,
        maxHeight: 200
    });

});
</script>

And your controller will not call a SelectList but a MultiSelect

ViewBag.Estagios = new MultiSelectList(_estagioService.ListarEstagios(), "EstagioId", "Nome", estagios);

Check the variable names to test.

    
03.08.2017 / 02:41
0
    //Você deve usar um MultiSelectList:
    ViewBag.itens = MultiSelectList(itens, "id", "Nome", itensSelected);


//E no html:
@Html.DropDownListFor(model => model.Estagios, (SelectList)ViewBag.itens, "Selecione")
    
03.08.2017 / 14:34