How to bind Dictionary<string, bool> to CheckBoxFor?

670 Views Asked by At

I am wondering if it is somehow possible to bind the value of a Dictionary to a CheckBoxFor?

I tried to do it like this:

View

@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>
}

Model

[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; }

Controller

If I set a checkbox and submit it, the given model to my controller doesn't have this value checked (true).

public ViewResult Index(TableModel model)
{
0

There are 0 best solutions below