ASP.NET MVC - Using checkboxes
Sunday 15, February 2015 | Post link
|
Download related material
One can't stop loving model binding in ASP.NET MVC. It simplifies so many things. There are some tricky areas though one of which is using checkboxes. In this post, I'll try to explain how I have used them.
Let's start with a simple example of a news-letter subscription. A user decides to subscribe or not subscribe for a news-letter subscription. A simple model class holds this information.
A simple selection screen allows the user to select or deselect this:
The view code (Choose.cshtml) is present below:
The controller (SubscriptionController.cs) code is present below:
Clicking the 'Submit' button takes the user to a the 'finalize' screen where we display his selection.
The view code (Finalize.cshtml) is present below:
MVC is able correctly map the boolean property 'Subscribe' of the model class with the result of selecting or not-selecting the check-box.
That was easy. Let's now change requirement a bit. Instead of just selecting whether he wants to subscribe or not, the user selects from a number of magazines the ones he wants to subscribe to. The new data model is:
The user starts the process from the ChooseMagazines view (ChooseMagazines.cshtml).
The view code for this below:
The controller code for this action is:
GetMagazines would ideally be implemented in the service/business layer. When the user submits his selection, in the controller's action method we manipulate the data returned in the model a bit:
Since html controls store their value as string, we defined the SelectedMagazineIds property as a string enumeration. You could use a list or an array and it would work just as well. When the form is posted to the method, SelectedMagazineIds will contain integer IDs only for the checked check-boxes. The check-boxes which were not checked will contain the value "False". All we need to do it extract the valid integer ids which are the magazine-ids the user selected. We do that in the following lines:
We then retrieve the complete magazine objects based on these ids and pass them to the FinalizeMagazines view for display.
The code for the view (FinalizeMagazines.cshtml) is below:
To summarize, even though there is no native support for multiple checkboxes for the same data-memeber, it's not difficult to use checkboxes is such situations.
Download related material
Let's start with a simple example of a news-letter subscription. A user decides to subscribe or not subscribe for a news-letter subscription. A simple model class holds this information.
A simple selection screen allows the user to select or deselect this:
The view code (Choose.cshtml) is present below:
The controller (SubscriptionController.cs) code is present below:
Clicking the 'Submit' button takes the user to a the 'finalize' screen where we display his selection.
The view code (Finalize.cshtml) is present below:
MVC is able correctly map the boolean property 'Subscribe' of the model class with the result of selecting or not-selecting the check-box.
That was easy. Let's now change requirement a bit. Instead of just selecting whether he wants to subscribe or not, the user selects from a number of magazines the ones he wants to subscribe to. The new data model is:
The user starts the process from the ChooseMagazines view (ChooseMagazines.cshtml).
The view code for this below:
The controller code for this action is:
GetMagazines would ideally be implemented in the service/business layer. When the user submits his selection, in the controller's action method we manipulate the data returned in the model a bit:
Since html controls store their value as string, we defined the SelectedMagazineIds property as a string enumeration. You could use a list or an array and it would work just as well. When the form is posted to the method, SelectedMagazineIds will contain integer IDs only for the checked check-boxes. The check-boxes which were not checked will contain the value "False". All we need to do it extract the valid integer ids which are the magazine-ids the user selected. We do that in the following lines:
We then retrieve the complete magazine objects based on these ids and pass them to the FinalizeMagazines view for display.
The code for the view (FinalizeMagazines.cshtml) is below:
To summarize, even though there is no native support for multiple checkboxes for the same data-memeber, it's not difficult to use checkboxes is such situations.
Categories:
ASP.NET MVC (4)
Programming (28)
Comments