I have a List in my ViewModel I parse to the View
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; }
}
I then want to print a checkbox for all the items in my list, so the list is in the ViewModel the view uses
@foreach(var boolSettingList in Model.BoolSettingList)
{
<div>
@Html.CheckBox(boolSettingList.DisplayName, boolSettingList.Value)
@boolSettingList.DisplayName
</div>
}
The problem is when I post this then my Model have not saved the updated settings (The 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 viewmodel will have many lists and some of them will have the same names! So that will cause conflicts
So is there a way to foreach print all my bools and then make MVC figure out to put the data back into the List object after? I cant get CheckBoxFor to work as it wants an expression and I cant figure out a way to itterate through my list that way
Can I maybe fix it with Templates, by making a template for BoolSetting and maybe List ?