开发者

asp.net mvc IDataErrorInfo validation when using ViewModel

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜