Retrieving MultiSelectList MVC values

1

I'm not sure how to handle data received via MultiSelectList . Also, I can only select more than one option in View if I press CTRL. Is there a way for the user to click on more than one value without necessarily holding the key?

View Create

<div>
  Combos @Html.ListBox("Combos", new MultiSelectList(ViewData["Combos"] as System.Collections.IEnumerable, "id", "nome", Model.Combos.Select(x => x.id).AsEnumerable()))
</div>

Populating MultiSelectList not Controller

CombosAplicacao bdCombos;
bdCombos = CombosAplicacaoConstrutor.CombosAplicacaoEF();
ViewData["Combos"] = bdCombos.ListarTodos();

Throughout the rest of my application, I work with SelectList , since I have a relationship usually 1 - n. Only in the case of Combos I have a many-to-many relationship.

In the case of SelectList work as follows:

View Create

@Html.DropDownList("Sala", ViewData["Sala"] as SelectList)

Populating with Controller

SalaAplicacao bdSala;
bdSala = SalaAplicacaoConstrutor.SalaAplicacaoEF();
var listSala = new SelectList(bdSala.ListarTodos(), "ID", "Nome");
ViewData["Sala"] = listSala;

Recovering and saving to the bank by Controller

 [AcceptVerbs(HttpVerbs.Post)]
 public PartialViewResult Create(CONGRESSO_Cursos cursos, FormCollection dados)
 {
     SalaAplicacao bdSala;
     bdSala = SalaAplicacaoConstrutor.SalaAplicacaoEF();
     cursos.CONGRESSO_Sala = bdSala.ListarPorId(dados["Sala"]);
     bdcursos.Salvar(cursos);
 } 
    
asked by anonymous 08.08.2014 / 15:52

1 answer

1

If you retrieve dados["Combos"] , it will recover all that have been selected, so just give a split.

var combos = dados["Combos"].ToString().Split(',');
foreach(var combo in combos){
   //vai percorrer todos os combos selecionados.
}
    
08.08.2014 / 16:19