tags:

views:

107

answers:

1

Hi,

I'm using ASP.NET MVC and I am trying to use a dictionary as a property in a ViewModel over which i want to iterate in the view and dynamically generate checkboxes for each key-value-pair in the Dictionary. I can successfully get the checkboxes to be generated with the correct values for the "value" attribute but i'm having trouble propagating the checked checkboxes back to the controller.

Here's the viewmodel:

public class DocumentAddModel
{
    private DocumentViewModel _documentViewModel;
    private Dictionary<int, string> categoryCheckboxes = new Dictionary<int, string>();
    private Dictionary<int, string> roleCheckboxes = new Dictionary<int, string>();
    private Dictionary<int, string> fundCheckboxes = new Dictionary<int, string>();

    public DocumentViewModel DocumentViewModel
    {
        get { return _documentViewModel; }
        set { _documentViewModel = value; }
    }

    // the key of the dictionary (int) is the id of a Category object and
    // the value of the dictionary (string) is the friendly description name 
    // of the Category object
    public Dictionary<int, string> CategoryCheckboxes
    {
        get { return categoryCheckboxes; }
        set { categoryCheckboxes = value; }
    }

    public Dictionary<int, string> RoleCheckboxes
    {
        get { return roleCheckboxes; }
        set { roleCheckboxes = value; }
    }

    public Dictionary<int, string> FundCheckboxes
    {
        get { return fundCheckboxes; }
        set { fundCheckboxes = value; }
    }
}

Here's the part of the View that generates the checkboxes:

<% foreach (KeyValuePair<int, string> categoryCheckbox in Model.CategoryCheckboxes){%>
    <input type="checkbox" name= "CategoryCheckboxes" value="<%= categoryCheckbox.Key %>" />
    <%= categoryCheckbox.Value%>
    <% } %>

However when i submit the form the CategoryCheckboxes Dictionary has a count of zero even though i checked certain categories. What am i doing wrong?

Thanks

P.S. I do not want to use Html.Checkbox (as i suspect someone might suggest that) since that is equally tricky to get working (i tried that first) and i really don't like the idea that it generates hidden fields.

+1  A: 

I don't think the default model binder can handle populating a Dictionary. Generally what I do in this sort of situation is have one property on the model for generating the controls and another for retrieving the selected values. I haven't done this with checkboxes, just dropdown lists, but the principle would be the same. You'd add a CategoryCheckboxes array (of type int) to your model and that would be populated when the form is posted. It would contain the values of the selected checkboxes. Similarly for your other types of checkboxes.

 public int[] CategoryCheckboxes { get; set; }
 public int[] RoleCheckboxes { get; set; }
 public int[] FundCheckboxes { get; set; }

I might name them (and the associated HTML inputs) differently, dropping the Checkboxes moniker, for instance.

tvanfosson