This is my View. How to use CheckboxFor():
@using eMCViewModels;
@model eMCViewModels.RolesViewModel
@{
ViewBag.Title = \"CreateNew\";
}
C
CheckBoxFor works with boolean properties only. So the first thing you need to do is to modify your view model in order to include a boolean property indicating whether the record was selected:
public class RoleAccessViewModel
{
public int RoleID { get; set; }
public string RoleName { get; set; }
public int MenuID { get; set; }
public string MenuDisplayName { get; set; }
public string MenuDiscription { get; set; }
public bool IsEnabled { get; set; }
}
and then I would recommend replacing your foreach loop with an editor template:
@Html.EditorFor(x => x.RoleAccess)
and finally write the corresponding editor template which will automatically be rendered for each element of the RolesAccess collection (~/Views/Shared/EditorTemplates/RoleAccessViewModel.cshtml):
@model RoleAccessViewModel
@Html.HiddenFor(x => x.RoleID)
... might want to include additional hidden fields
... for the other properties that you want to be bound back
@Html.LabelFor(x => x.IsEnabled, Model.RoleName)
@Html.CheckBoxFor(x => x.IsEnabled)