Recovering Multiselectlist MVC values

Asked

Viewed 204 times

1

I’m not sure how to handle the received data via MultiSelectList. Also, I can only select more than one option on View if I press CTRL. There is a way the user can click more than one value without necessarily holding this 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 in the Controller

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

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


In the case of SelectList work as follows:

View Create

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

Populating Controller

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

Retrieving and saving in 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);
 } 

1 answer

1


If you recover the dados["Combos"], will recover all that were selected, so just take a split.

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

Browser other questions tagged

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