I am fairly new to MVC so please don't hesitate to suggest a better/cleaner/simpler way of achieving what I am trying to do in a more "MVC" friendly fashion.
Here goes...
To make this easier I will use a concrete example using movies and genres. A genre can be associated with many movies. Genres can increase over time and so can movies.
The use case is as follows. The user wants to filter movies by selecting his/her desired genres.
So, I have a page where at the top there is a section where the user can pick any combination of genres to filter on using checkboxes. Below that is a list of movies that are in those genres.
In MVC I am trying to implement this using a ViewPage(of IDictionary(of Genre, Boolean)) where the boolean is whether or not a particular Genre has been selected for filtering.
I then go into loop that creates checkboxes for them to select:
<% for each genre As KeyValuePair(Of Genre, Boolean) in Model %>
<%=Html.Hidden("genre[" & i & "].Key", genre.Key.ID)%>
<%=Html.CheckBox("genre[" & i & "].Value", genre.Value)%>
<%=Html.TextBox("genre[" & i & "].Key.Name", genre.Key.Name)%>
<% Next %>
I have stripped out all formatting to focus on the meat of the issue. So in the Dictionary genre.Value would return whether the checkbox is selected or not.
After that there is a chunk of code that displays a list of Movies depending on which Genres were selected.
My problem is when I try to post back (not using Ajax for now) the action has a Null value in the dictionary:
<AcceptVerbs(HttpVerbs.Post)> _
Function FilterSelected(ByVal selectedGenres As IDictionary(Of MarketCategory, Boolean)) As ActionResult
Return View("Index", theView)
End Function
Can anyone tell me what I am missing? Also, I am aware this can be done using Ajax and I will most likely do it using Ajax but I would still like to know why this doesn't work.
Thanks ;-)