字典<short,Dictionary >模型绑定不起作用
我无法将模型绑定到Controller。 请给我任何建议。 模型,控制器和视图类如下。 当模型被子类化时,字典属性等于null。
public class GroupRights //model { public List groups { get; set; } public Dictionary<short, Dictionary> groupRights { get; set; } // group function HasPermission } public enum EnFunction { LPDU_login, LPDU_changePassword, LPDU_transitList, LPDU_PosEventList, .... }
调节器
public ActionResult GroupRights() { TocCommonService.CommonServiceClient client = new TocCommonService.CommonServiceClient(); GroupRights gr = new GroupRights(); gr.groups = client.GetAllOperatorGroups().ToList(); gr.groupRights = new Dictionary<short, Dictionary>(); foreach (var g in gr.groups) { Dictionary permission = new Dictionary(); foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast()) { permission.Add(func, client.hasPermission(new DtoGrup() { GROUPID = g.GROUPID }, func)); } gr.groupRights.Add(g.GROUPID, permission); } return View(gr); }
视图
@model TocWebApplication.Models.GroupRights @{ int id = 0; } @using (Html.BeginForm("ChangePermissionOfGroup", "Home", FormMethod.Post)) { @foreach (var gr in Model.groups) { @gr.GROUPNAME (@gr.GROUPID) } @foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast()) { @(func.ToString()) @for (int j = 0; j < Model.groups.Count(); j++) { @Html.CheckBoxFor(model => model.groupRights[Model.groups[j].GROUPID][func]) } }
}
对于Key
和Value
都是简单值类型的所有字典(例如Dictionary
), DefaultModelBinder
要求name / value属性采用格式(在您的情况下)
没有HtmlHelper
方法可以生成正确的属性并绑定到您手动生成html所需的groupRights
。
相反,创建一个表示要在视图中显示的数据的视图模型。如果我已正确解释了这一点,则需要显示一个表格,向下显示每个EnFunction
值,并显示每个EnFunction
,并显示一个复选框在每个单元格中确定为每个DtoGrup
选择DtoGrup
。
public class GroupRightsVM // represents a table cell { public short GroupID { get; set; } public bool IsSelected { get; set; } } public class RightsVM // represents a table row { public RightsVM() { Groups = new List() } public EnFunction Right { get; set; } public List Groups { get; set; } // represents the cells in a row } public class PermissionsVM // Represents the table { public PermissionsVM() { Permissions = new List() } public IEnumerable Headings { get; set; } // represents the table headings public List Permissions { get; set; } // represents all rows }
在视图中
@foreach(var heading in Model.Headings) { @heading } @for(int i = 0; i < Model.Rows.Count; i++) { @Html.HiddenFor(m => m.Permissions[i].Right) @Html.DisplayFor(m => m.Permissions[i].Right) @for(int j = 0; j < Model.Permissions[i].Groups.Count; j++) { @Html.HiddenFor(m => m.Permissions[i].Groups[j].GroupID) @Html.CheckboxFor(m => m.Permissions[i].Groups[j].IsSelected) } }
在GET方法中,初始化PermissionsVM
的实例并根据您的数据模型填充它并将其传递给视图,例如
var groups = client.GetAllOperatorGroups() var model = new PermissionsVM(); model.Headings = groups.Select(x => String.Format("{0} {{1})", x.GROUPNAME, x.GROUPID)); foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast()) { RightsVM r = new RightsVM(); r.Right = func; foreach (var g in groups) { GroupRightsVM gr = new GroupRightsVM(); gr.GroupID = g.GROUPID; gr.IsSelected = ... r.Groups.Add(gr); } model.Persmissions.Add(r); } return View(model);
并在POST方法中
public ActionResult GroupRights(PermissionsVM model)