Extending the MVC RequiredAttribute

前端 未结 1 703
暖寄归人
暖寄归人 2020-12-10 06:06

I have an extended class of RequiredAttribute that doesn\'t send error messages back. If I check it in the debugger the text is there alright.

public class V         


        
相关标签:
1条回答
  • 2020-12-10 06:32

    I also had a problem with client side validation when I created my own derivative of the RequiredAttribute. To fix it you need to register your data annotation like so:

    DataAnnotationsModelValidatorProvider.RegisterAdapter(
                typeof(VierRequired),
                typeof(RequiredAttributeAdapter));
    

    Simply call this in your Application_Start() method and client side validation should work as normal.

    If your attribute is not working when you are POST-ing your form then this would indicate to me that there is something wrong with the logic in your attribute (check you IsValid method). I am also not sure what you are trying to achieve with your derived data annotation; your logic looks like it is trying to do pretty much what the default attribute does anyway:

    Taken from the MSDN documentation:

    A validation exception is raised if the property is null, contains an empty string (""), or contains only white-space characters.

    0 讨论(0)
提交回复
热议问题