Create MVC3 CheckBoxFor from the <T> list and get the list back (with updated values) in the message
I have a list in my ViewModel. I understand the presentation
List<BoolSetting> BoolSetting:
public class BoolSetting { public BoolSetting(string displayName, bool value) { DisplayName = displayName; Value = value; } public string DisplayName { get; set; } public bool Value { get; set; } } Then I want to print a check box for all the items in my list, so the list is in the ViewModel, the view is used
@foreach(var boolSettingList in Model.BoolSettingList) { <div> @Html.CheckBox(boolSettingList.DisplayName, boolSettingList.Value) @boolSettingList.DisplayName </div> } The problem is when I submit this, then my model did not save the updated settings (bool values) in the list in my ViewModel, and therefore the object is empty.
I could do
foreach (var VARIABLE in userSettingConfigViewModel.BoolSettingList) { VARIABLE.Value = (bool)Request.Form[VARIABLE.DisplayName]; } But this view model will have many lists, and some of them will have the same name! It will cause conflicts
So, is there a way to print all my bools and then make MVC to return the data back to the List object after? I cannot get CheckBoxFor to work the way it wants expressions, and I cannot find a way to list through my list this way
Can I fix this with templates by creating a template for BoolSetting and possibly List?
Start by fixing your view model and deleting the default user designer or middleware, you wonβt be able to create it, and you will have to write your own attachments and more:
public class BoolSetting { public string DisplayName { get; set; } public bool Value { get; set; } } public class MyViewModel { public List<BoolSetting> Settings { get; set; } } Then write a controller action that populates your view model:
public class HomeController : Controller { public ActionResult Index() { var model = new MyViewModel { Settings = new[] { new BoolSetting { DisplayName = "name 1", Value = true }, new BoolSetting { DisplayName = "name 2", Value = false }, new BoolSetting { DisplayName = "name 3", Value = true }, }.ToList() }; return View(model); } [HttpPost] public ActionResult Index(MyViewModel model) { return View(model); } } then the view ( ~/Views/Home/Index.cshtml ), in which you simply use the editor templates and do not write any foreach loops or weakly typed html helpers such as Html.CheckBox . Using the editor templates, you will make sure that all input fields have the correct names so that the default communication device can retrieve its values ββinto the presentation model during postback:
@model MyViewModel @using (Html.BeginForm()) { @Html.EditorFor(x => x.Settings) <button type="submit">OK</button> } and finally, the corresponding editor template for the view model, which will be displayed for each element of the collection ( ~/Views/Home/EditorTemplates/BoolSetting.cshtml ):
@model BoolSetting <div> @Html.CheckBoxFor(x => x.Value) @Html.LabelFor(x => x.Value, Model.DisplayName) @Html.HiddenFor(x => x.DisplayName) </div>