Model Binding and Validation ASP.NET MVC Sample Project
I wanted to share this sample project I put together that demonstrates model-binding and validation of several form components, including complex ones such as dropdowns, checkboxes, and radio buttons. There are several aspects to think about when submitting a form:
- HTML form value has to map to the correct C# property/object via Model Binding.
- Value submitted has to be properly validated based on Attributes or logic in controller.
- If the form has invalid data, the form has to refresh itself and be populated with all the values the user previous attempted to submit.
- The appropriate error messages have to be displayed next. The error messages can be part of the set of attributes above the property or handled via ViewBag/ViewData object.
The following example has all that. It also uses a ViewModel (BankAccount.cs) to piece together the main Business Domain Models (e.g. Person).
Here’s our classes in the project:
Categories