如何将Dictionary <string,bool =“”>绑定到CheckBoxFor?

时间:2018-07-26 12:42:28

标签: c# asp.net-mvc dictionary checkboxfor

我想知道是否可以将Dictionary的值绑定到CheckBoxFor吗?

我试图这样做:

查看

@for (int c = 0; c < Model.ObjectsWithPermission[a].PermissionColumns.Count; c++)
{
    <tr>
        <td>
            @Model.ObjectsWithPermission[a].PermissionColumns[c].Name
        </td>
        @for (int i = 0; i < Model.ObjectsWithPermission[a].PermissionColumns[c].Permissions.Count; i++)
        {
            <td align="center">
                @*@Html.CheckBox(permission.Key, permission.Value)*@
                @Html.CheckBoxFor(x => Model.ObjectsWithPermission[a].PermissionColumns[c].Permissions.ElementAt(i).Value)
        </td>
}

型号

[Display(Name = "Groups")]
public PermissionMatrix GroupsWithPermission { get; set; }

PermissionMatrix

public string Name { get; set; }
public List<PermissionMatrixColumn> PermissionColumns { get; set; }

PermissionMatrixColumn

private void Init()
{
    //I want to bind it to these values
    Permissions = new Dictionary<string, bool>() 
    {
        {"SELECT",false},
        {"INSERT",false},
        {"UPDATE",false},
        {"ALTER",false},
        {"DELETE",false},
    };
}

public PermissionMatrixColumn(string name)
{
    Init();
    Name = name;
}

public PermissionMatrixColumn()
{
    Init();
}

public string Name { get; set; }

[Display(Name = "Permissions")]
public Dictionary<string, bool> Permissions { get; set; }

控制器

如果我设置了一个复选框并提交,则给控制器的给定模型未选中此值(true)。

public ViewResult Index(TableModel model)
{

0 个答案:

没有答案