Do not use the dictionary for this. They do not fit very well with model binding. Maybe PITA.
A view model would be more appropriate:
public class MyViewModel { public string Id { get; set; } public bool Checked { get; set; } }
then the controller:
public class HomeController : Controller { public ActionResult Index() { var model = new[] { new MyViewModel { Id = "A", Checked = true }, new MyViewModel { Id = "B", Checked = false }, }; return View(model); } [HttpPost] public ActionResult Index(IEnumerable<MyViewModel> model) { return View(model); } }
then the corresponding view ( ~/Views/Home/Index.cshtml ):
@model IEnumerable<MyViewModel> @using (Html.BeginForm()) { <table> <thead> <tr> <th></th> </tr> </thead> <tbody> @Html.EditorForModel() </tbody> </table> <input type="submit" value="Save" /> }
and finally, the corresponding editor template ( ~/Views/Home/EditorTemplates/MyViewModel.cshtml ):
@model MyViewModel <tr> <td> @Html.HiddenFor(x => x.Id) @Html.CheckBoxFor(x => x.Checked) @Html.DisplayFor(x => x.Id) </td> </tr>
source share