开发者

View model properties has changing validation rules at run time

I'm new to C# MVC and I'm trying to add some dynamic validation checks to my view models that are used in a form. For example, I have a string property called FirstName. I can add the attribute StringLength(10) and Required() to it.

My problem is, depending on some other field, the FirstName StringLength could var开发者_如何学Cy from 10 to 20, etc. I still want to use the MVC validations but be able to modify it. I know that attributes are bound to the class so maybe I'm using the wrong thing.

I want the abilities for attribute validation but have it modifiable at run time. Is this possible?


The values in an attribute have to be literals. You can still use attribute based validation, but you will need to use the CustomValidation tag and point it at a method to use. If it depends on multiple fields in the object, you will want to put this on the class rather than the property.


It seems you can add validation attributes at runtime by implementing DataAnnotationsModelValidatorProvider: Dynamic Attributes @ forums.asp.net

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜