I’m trying to use the built in ASP.NET MVC 2 client side validation on a Select List like the following:
private SelectList _CategoryList;
[Required(ErrorMessage = "Category Required")]
[System.ComponentModel.DataAnnotations.Range(1, double.MaxValue, ErrorMessage = "Please Select A Category")]
[DisplayName("Category")]
public SelectList CategoryList
{
get
{
return new SelectList(Categories, "CatID", "CatFullName"); ;
}
set
{
_CategoryList = value;
}
}
However it’s not working…if the default value which is 0 is selected the validation message does not appear and the page progresses as though it’s validated. Thoughts?
Ok so I found the answer in an answer to a slightly different question. So I’m posting my complete code here, which extends on Scott Guthries ASP.NET MVC 2 Validation post: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
My ViewModel:
My Color class:
My list helper extensions stolen from Rob Connery, who stole it from someone else:
My Controller Code (yes it could be refactored to be more DRY):
My page markup: