views:

41

answers:

1

I have used IDataErrorInfo Validation for my Model. But when I use these model classes inside a view model, the validation does not happen.

sample viewmodel below

public class CategoryViewModel    
{
    // Category class with IDataErrorInfo
    public Category category { set; get; }

    // Subcategory class with IDataErrorInfo
    public IList<SubCategory> subcategory { set; get; }
}

Now, if Category or Subcategory classes are directly used as models for view, the validation works fine. But, if CategoryViewModel is used, no validation occurs.

A: 

IDataErrorInfo doesn't work with child properties. You will need to implement this interface by the view model you are binding to (CategoryViewModel). It is also considered as bad practice. As an alternative you might look at DataAnnotations or FluentValidation for more advanced validation scenarios.

Darin Dimitrov