Dictionary<short, Dictionary<EnFunction, bool>> model binding not work

本小妞迷上赌 提交于 2019-11-29 16:41:01

For all but Dictionaries where both the Key and Value are simple value types (for example Dictionary<string, bool>), the DefaultModelBinder requires the name/value attributes to be in the format (in your case)

<input name="groupRights[0].Key" value="1" ... />
<input name="groupRights[0].Value[0].Key" value="LPDU_login" ... />
<input name="groupRights[0].Value[0].Value" value="True" ... />
<input name="groupRights[1].Key" value="2" ... />
<input name="groupRights[1].Value[0].Key" value="LPDU_changePassword" ... />
<input name="groupRights[1].Value[0].Value" value="False" ... />

There are no HtmlHelper methods that can generate the correct attributes and to bind to your groupRights you would need to generate the html manually.

Instead, create a view model(s) that represents the data you want to display in the view.If I have interpreted this correctly, you want to display a table displaying each EnFunction value down, and each DtoGrup across, and to display a checkbox in each cell to determine which EnFunction is selected for each 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<GroupRightsVM>()
    }
    public EnFunction Right { get; set; }
    public List<GroupRightsVM> Groups { get; set; } // represents the cells in a row
}
public class PermissionsVM // Represents the table
{
    public PermissionsVM()
    {
        Permissions = new List<RightsVM>()
    }
    public IEnumerable<string> Headings { get; set; } // represents the table headings
    public List<RightsVM> Permissions { get; set; } // represents all rows
}

And in the view

<thead>
    <tr>
        <th></th>
        @foreach(var heading in Model.Headings)
        {
            <th>@heading</th>
        }
    </tr>
</thead>
<tbody>
    @for(int i = 0; i < Model.Rows.Count; i++)
    {
        <tr>
            <td>
                @Html.HiddenFor(m => m.Permissions[i].Right)
                @Html.DisplayFor(m => m.Permissions[i].Right)
            </td>
            @for(int j = 0; j < Model.Permissions[i].Groups.Count; j++)
            {
                <td>
                    @Html.HiddenFor(m => m.Permissions[i].Groups[j].GroupID)
                    @Html.CheckboxFor(m => m.Permissions[i].Groups[j].IsSelected)
                </td>
            }
        </tr>
    }
</tbody>

In the GET method, initialize an instance of PermissionsVM and populate it based on your data models and pass it to the view, for example

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<EnFunction>())
{
    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);

and in the POST method

public ActionResult GroupRights(PermissionsVM model)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!