2

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)
{
David Walser
  • 183
  • 20
  • You need to create a for loop and use an indexer instead of foreach. – Wheels73 Jul 26 '18 at 12:55
  • First, using a `foreach` loop will not work - you need to use a `for` loop or `EditorTempate` (refer [Post an HTML Table to ADO.NET DataTable](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) –  Jul 26 '18 at 12:55
  • But using a `Dictionary` is a bad idea. Use a collection of a view model containing properties `string Name` (for "SELECT", "INSERT" etc) and `bool IsSelected` to bind the checkbox to –  Jul 26 '18 at 12:57
  • @StephenMuecke a dictionary fits perfect for the rest of the code.. :/ – David Walser Jul 26 '18 at 13:00
  • @Wheels73 still not working, edited my question aswell (added for loop) – David Walser Jul 26 '18 at 13:02
  • Your editing data, so always create a view model to represent the data in the view (and a `Dictionary` is not a good fit). And read the link I gave you - you cannot use `ElementAt()` and you still have the outer `foreach` loop which will not work –  Jul 26 '18 at 13:03
  • @StephenMuecke but the outer foreach is working, it is showing me the `@item.Name`s? – David Walser Jul 26 '18 at 13:05
  • No it is NOT! Read the link to understand how the `name` attributes relate to binding to your model - your `CheckBoxFor()` is generating a `name` attribute that has no relationship to the model and will not bind (because of both your `foreach` loop and the `ElementAt()`) –  Jul 26 '18 at 13:07
  • what is wrong with `ElementAt()`???? – David Walser Jul 26 '18 at 13:09
  • Again, read the link and then look at the `name` attributes you are generating –  Jul 26 '18 at 13:13
  • @StephenMuecke I updated my question (still not working), but to be honest I still don't know the difference between `[i]` or `ElementAt(i)` – David Walser Jul 26 '18 at 13:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176806/discussion-between-stephen-muecke-and-david-walser). –  Jul 26 '18 at 13:25

0 Answers0