I have a List of about 20 items I want to display to the user with a checkbox beside each one (a Available property on my ViewModel).
When
Model:
public class MyViewModel
{
public int Id { get; set; }
public bool Available { get; set; }
}
Controller:
public class HomeController: Controller
{
public ActionResult Index()
{
var model = Enumerable.Range(1, 20).Select(x => new MyViewModel
{
Id = x
});
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable model)
{
...
}
}
View ~/Views/Home/Index.cshtml:
@model IEnumerable
@using (Html.BeginForm())
{
@Html.EditorForModel()
}
Editor template ~/Views/Home/EditorTemplates/MyViewModel.cshtml:
@model AppName.Models.MyViewModel
@Html.HiddenFor(x => x.Id)
@Html.CheckBoxFor(x => x.Available)