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.