Reputation: 125
So, I am trying to pass the chosen multiselect values throught a DropDownList and a BeginForm. Don't want to pass with javascript/ajax. The chosen plugin is working fine, show me the entries like i want. But I'm getting null values on controller:
Model
public class SorteioEspecial
{
RepositoryService service = new RepositoryService();
public SorteioEspecial()
{
funcionario = new List<Funcionario>();
ponderacaoFuncionario = new List<PonderacaoFuncionario>();
SelectedIds = new List<int>();
}
public int Id { get; set; }
public IEnumerable<Funcionario> funcionario { get; set; }
public IEnumerable<PonderacaoFuncionario> ponderacaoFuncionario { get; set; }
public List<int> SelectedIds { get; set; }
public IEnumerable<Funcionario> GetFuncionarios()
{
funcionario = service.GetFuncionarios();
return funcionario;
}
public IEnumerable<PonderacaoFuncionario> GetPonderacaoFuncionario()
{
ponderacaoFuncionario = service.GetPonderacaoFuncionario();
return ponderacaoFuncionario;
}
}
Controller
[HttpGet]
public ActionResult EscolherFuncionarios()
{
var sorteioEspecial = new SorteioEspecial();
List<Funcionario> list = new List<Funcionario>();
list = sorteioEspecial.GetFuncionarios().ToList().OrderBy(x => x.Nome).ToList();
ViewBag.FuncionarioId = new SelectList(list, "Id", "Nome");
return View(sorteioEspecial);
}
[HttpPost]
public ActionResult EscolherFuncionarios(List<int> SelectedIds)
{
return View();
}
View
@model Apdd.Models.SorteioEspecial
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Escolha os funcionários a ir a sorteio</h2>
@using (Html.BeginForm())
{
@Html.DropDownList("FuncionarioId", null, htmlAttributes: new { @class = "chosen-select", @data_placeholder = "Pick one!", @multiple = "true" })
<input type="submit" value="save" />
}
<script src="~/Scripts/jquery-2.1.1.js"></script>
<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/chosen.proto.js"></script>
<link href="~/Scripts/chosen.css" rel="stylesheet" />
<script src="~/Scripts/chosen.jquery.js"></script>
<script>
$(".chosen-select").chosen({
disable_search_threshold: 10,
no_results_text: "None!",
width: "95%"
});
</script>
The values in the ViewBag are a list of entities (Id, Name, and some more parameters), and for what I have been seing, the chose only passes the Id, exactly What I want. What I have to do to pass the values to controller?
Upvotes: 0
Views: 1859
Reputation: 902
The values are posted to the controller, bot the problem is with controller parameter binding, because the names from the parameter and the drop down list is different. If you change the name of the controller parameter list from "SelectedIds" to drop down list name "FuncionarioId" it should work. So change this:
[HttpPost]
public ActionResult EscolherFuncionarios(List<int> SelectedIds)
{
return View();
}
to this:
[HttpPost]
public ActionResult Test(List<int> FuncionarioId)
{
return View();
}
Upvotes: 0
Reputation: 62498
First of all the selected items will be posted as comma seperated string, so you need to bind your dropdownlist with a string property.
Secondly your dropdownlist name is FuncionarioId
so it will post in that key of FormCollection
while you have parameter in your action which of type List<int>
If you change your action signatures and check in form collection, you will find the comma seperated values in FormCollection
:
[HttpPost]
public ActionResult EscolherFuncionarios(FormCollection form)
{
string selectedValues = form["FuncionarioId"];
return View();
}
You can also refer to this article of Rick Anderson where he explained how to use Multi Select
Upvotes: 1