How to validate combox by using Microsoft MVC, Data Annotations Validation

4/1/2014·1 min read

This's article is about how to validate your combox by MVC's validation. There's something I just doing in my way, like save List of value to Lazy singleton. You could use ViewData too.

First, if you want to try this validation

Setting up data

The way I'm doing here is setting up data and Lazy singleton. You could create your own singleton class to set up data too.

Add property to your View Model

After set up data, I add two property to my View Model. One is actual data, which name is "CountryData". Another one is using to post and validation. name is "CountryList".

set up your view(razor)

Following is how to set up value to your razor.

You could go to following page to check demo.

DEMO

If you're first add mvc validation,you might need to add some setting to your file. You could reference following blog to check it out.

MVC Validation Setting

I'll add client custom validation in the future.