I want to define a view which displays a list of label and checkbox, user can change the checkbox, then post back. I have problem posting back the dictionary. That is, The d
Don't use a dictionary for this. They don't play well with model binding. Could be a PITA.
A view model would be more appropriate:
public class MyViewModel
{
public string Id { get; set; }
public bool Checked { get; set; }
}
then a 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 model)
{
return View(model);
}
}
then a corresponding view (~/Views/Home/Index.cshtml):
@model IEnumerable
@using (Html.BeginForm())
{
@Html.EditorForModel()
}
and finally the corresponding editor template (~/Views/Home/EditorTemplates/MyViewModel.cshtml):
@model MyViewModel
@Html.HiddenFor(x => x.Id)
@Html.CheckBoxFor(x => x.Checked)
@Html.DisplayFor(x => x.Id)