我想实现一个许可屏幕中,用户可以给一个特定的屏幕上特定权限。 对于这个我生成Checkboxfor的集合,势必布尔属性的集合。 但是,当我提交表单,我要么让所有的布尔属性真或假都取决于我是否初始化为真或假的视图模型构造这些属性。
下面是视图模型的代码:
的方法我:
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
});
}
}
}
}
办法之二
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);
}
}
}
}
这里是代码我的看法:
的方法我:
@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>
}
办法之二:
@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>
}
下面是在控制器创建actionmethod的代码:
[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");
}
当我检查视图模型的创建actionmethod,所有IsAllowed性能都是假的。 正如在视图模型构造器中初始化。 没有办法检查/从视图中取消选中复选框的效果。
我缺少的东西吗?