I'm trying to implement a permission screen in which a user can be given a particular permission on a particular screen. For this I'm generating a collection of Checkboxfor, bound to a collection of bool properties. But when I submit the form, I'm either getting all bool properties true or all false, depending on whether I initialized these properties as true or false in the viewmodel constructor.
Here is the code for the ViewModel:
Approach I:
public class MyViewModel
{
public MyModel Model { get; set; }
public IEnumerable<ScreenType> Screens { get; set; }
public IEnumerable<SecurityType> SecurityTypes { get; set; }
public List<PermissionType> Permissions { get; set; }
public MyViewModel()
{
LoadScreens();
LoadSecurityTypes();
LoadPermissions();
}
public void LoadPermissions()
{
Permissions = new List<PermissionType>();
foreach (var screen in Screens)
{
foreach (var securityType in SecurityTypes)
{
Permissions.Add(
new PermissionType
{
PermissionId= Guid.NewGuid(),
ScreenId= screen.Id,
SecurityId = securityType.Id,
IsAllowed = false
});
}
}
}
}
Approach II
public class MyViewModel
{
public MyModel Model { get; set; }
public IEnumerable<ScreenType> Screens { get; set; }
public IEnumerable<SecurityType> SecurityTypes { get; set; }
public List<bool> AllowedList { get; set; }
public MyViewModel()
{
LoadScreens();
LoadSecurityTypes();
LoadPermissions();
}
public void LoadPermissions()
{
AllowedList = new List<bool>();
foreach (var form in Forms)
{
foreach (var security in Securities)
{
AllowedList.Add(false);
}
}
}
}
Here is the code my the view:
Approach I:
@using (Ajax.BeginForm("Create", "Role", null, new AjaxOptions { UpdateTargetId = "addStatus", InsertionMode = InsertionMode.Replace, OnSuccess = "onFormPostSuccess" }, new { @id = "AddForm" }))
{
<div>
<span><label>Screen</label></span>
@foreach (var security in Model.SecurityTypes)
{
<span><label>@security.Name</label></span>
}
<br />
@foreach (var screen in Model.Screens)
{
<span>@screen.Name</span>
foreach (var security in Model.SecurityTypes)
{
<span>@Html.CheckBoxFor(m => m.Permissions.Where(s => s.SecurityId == security.Id && s.ScreenId == screen.Id).Single().IsAllowed, new { @id = HtmlHelper.GenerateIdFromName("Create" + screen.Name + security.Name) })</span>
}
<br />
}
</div>
<div>
<span>
<input type="image" src="/content/images/submit_button.png" value="submit" />
</span>
</div>
<div>
<span id="addStatus" class="submitStatus"></span>
</div>
}
Approach II:
@using (Ajax.BeginForm("Create", "Role", null, new AjaxOptions { UpdateTargetId = "addStatus", InsertionMode = InsertionMode.Replace, OnSuccess = "onFormPostSuccess" }, new { @id = "AddForm" }))
{
<div>
<span><label>Screen</label></span>
@foreach (var security in Model.SecurityTypes)
{
<span><label>@security.Name</label></span>
}
<br />
@foreach (int i=0; i < Model.Screens.Count(); i++)
{
<span>@Model.Screens.ElementAt(i).Name</span>
foreach (int j=0; j<Model.SecurityTypes.Count(); j++)
{
@* The index 5*i+j is because I have 5 security types *@
<span>@Html.CheckBoxFor(Model.AllowedList[5*i+j], new { @id = HtmlHelper.GenerateIdFromName("Create" + @Model.Screens.ElementAt(i).Name + @Model.SecurityTypes.ElementAt(j).Name) })</span>
}
<br />
}
</div>
<div>
<span>
<input type="image" src="/content/images/submit_button.png" value="submit" />
</span>
</div>
<div>
<span id="addStatus" class="submitStatus"></span>
</div>
}
Here's the code for the Create actionmethod in Controller:
[Authorize]
[HttpPost]
public JsonResult Create(MyViewModel viewModel)
{
if ( ModelState.IsValid)
{
if (service.AddRole(viewModel))
{
return Json("Role Added !");
}
return Json("Role exists !");
}
return Json("Please correct errors");
}
When I check the viewModel in the Create actionmethod, all the IsAllowed properties are false. As were initialized in the viewmodel contructor. There is no effect of checking/unchecking Checkboxes from the view.
Am I missing something?
Strongly typed HTML helpers such as the CheckBoxFor work only with simple expressions as first argument (property and array index access at most). The following expression is something completely out of the capacities of this helper:
I would recommend you to use a real view model and perform this at your controller level when mapping between your domain model and the view model.
UPDATE:
Apparently my answer about view models wasn't clear enough, so let me try to exemplify what I mean by view models.
So we start by defining our view models that will be required for implementing the required logic for this view:
Then we could have a controller action that will take care of fetching the domain models from a repository or something and map to the view models:
and now what's left is to define the view and the corresponding editor/display templates.
Let's start with the main view (
~/Views/Home/Create.cshtml
):Next we have an editor template for the
ScreenViewModel
model (~/Views/Shared/EditorTemplates/ScreenViewModel.cshtml
):Then the editor template for the
SecurityTypeViewModel
model (~/Views/Shared/EditorTemplates/SecurityTypeViewModel.cshtml
):And finally the display template for the
SecurityTypeViewModel
model (~/Views/Shared/DisplayTemplates/SecurityTypeViewModel.cshtml
):And that's pretty much it:
I have left for you the mapping between your actual domain models and the view models defined here.