The fact is, since in your main view you have IEnumerable<MyViewModel> , the prefix is [0].Selected , which the helpers do not like. You will also notice that your flag does not have an identifier. Here's a hack that you can use to change this prefix:
@model MyViewModel @{ ViewData.TemplateInfo.HtmlFieldPrefix = "Model" + ViewData.TemplateInfo.HtmlFieldPrefix; } <p> @Html.LabelFor(model => model.Selected, Model.Name) @Html.EditorFor(model => model.Selected) </p>
Obviously, this assumes your POST action argument is called model :
[HttpPost] public ActionResult Index(IEnumerable<MyViewModel> model) { ... }
which may not always be so.
Also note that instead of @Html.DisplayFor(x => Model) in the main view, you can use @Html.EditorForModel() .
Another possibility is to wrap this in a view model with the collection property:
public class MyViewModel {public IEnumerable Items {get; set; }}
where ItemViewModel :
public class ItemViewModel { public string Name { get; set; } public bool Selected { get; set; } }
and then your controller:
public ActionResult Index() { var model = new MyViewModel { Items = new[] { new ItemViewModel() { Name= "Foo" }, new ItemViewModel() { Name= "Bar" } } }; return View(model); } [HttpPost] public ActionResult Index(MyViewModel model) { ... }
and in view:
@model MyViewModel <div> @using (Html.BeginForm()) { @Html.DisplayFor(x => x.Items) <button type="submit">OK</button> } </div>
and in the editor template ( ~/Views/Shared/EditorTemplates/ItemViewModel.cshtml ):
@model ItemViewModel <p> @Html.LabelFor(model => model.Selected, Model.Name) @Html.EditorFor(model => model.Selected) </p>
Now it will work as expected.