Dropdownlistfor selected values do not persist

Asked

Viewed 262 times

2

I am using Asp.net C# with mvc 4 and in my View I have a Dropdownlistfor, with a class I used of bootstrap-multiselect, as in the image:

inserir a descrição da imagem aqui

And the code in the view:

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

When I do the search, I do a Post in a method in the Controller and within that method (both get and post) feed the Viewmodel and the Selectlist:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

So, he’s taking the values I posted, which are 1, 3 and 5. I give a Return View passing to Viewmodel. When he posts and the page reloads, he assumes the value in the right querystring and does all the processing of the right search as programmed, but he does not assume the values selector in Dropdownlist, bringing only the first of the list: inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I’m doing something wrong?

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

2 answers

1


I don’t know if this way of yours works with Multiselect.

Do it like this, 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..

  • 1

    I managed to, but I made some changes to what you gave me. In the controller I kept: Viewmodel.Interns = new Selectlist(_internsService.Listings(), "Interns", "Names", interns); In View: @Html.Listbox("Interns", Model.Interns, new { @class = "multiselect-group-clickable" }) I put the Jquery function that you passed and worked correctly. Thank you so much for your help!

  • If you think it helped, you can take it as an answer, please.

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")
  • Your answer is not clear enough. Prepare a response with context, explain what is wrong and point out the changed items. Be clear and always respond with as much detail as possible.

Browser other questions tagged

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