I have a view that contains quite many select
elements (in the form of @Html.DropDownList
or @Html.DropDownListFor
). The problem is that they are arranged in a table
-like manner and doubly-indexed (the number of rows and columns changes depending on the data).
It is only possible to use single-indexed properties/fields to bind to the selected value of the DropDownListFor
helper, and the number of properties I'd need varies, so I wonder:
Is there an ASP.NET MVC way to get the selected values in the controller?
That is, I would now use jQuery to build some (maybe JSON) data to send to the controller manually. But first I'd like to know if there is something else I could try :)
Example project: View:
@model DropDownMatrixTest.Models.MatrixViewModel
@using (Html.BeginForm("Foo", "Home", FormMethod.Post))
{
<table>
<tbody>
@for (int i = 0; i < 10; i++)
{
<tr>
<td>@i-th row:</td>
@for (int j = 0; j < 4; j++)
{
<td>
@Html.DropDownListFor(m => m.SelectedValues[@i, @j],
Model.Foos.Select(x => new SelectListItem
{
Text = x.Text,
Value = x.Id.ToString()
}))
</td>
}
</tr>
}
</tbody>
</table>
<button type="submit">Submit</button>
}
ViewModel:
public class MatrixViewModel
{
public IEnumerable<Foo> Foos { get; set; }
public int[,] SelectedValues { get; set; } // I know this wouldn't work
}
Controller methods:
public ActionResult Index()
{
MatrixViewModel vm = new MatrixViewModel
{
Foos = Enumerable.Range(1, 10).Select(x => new Foo { Id = x, Text = "Foo " + x })
};
return View(vm);
}
[HttpPost]
public ActionResult Foo(MatrixViewModel vm)
{
// Here is where I'd like to get the selected data in some form
return View("Index", vm);
}