Conditionally validating portions of an ASP.NET MVC Model with DataAnnotations?

后端 未结 7 1972
不思量自难忘°
不思量自难忘° 2020-12-30 10:12

I have certain panels on my page that are hidden under certain circumstances.

For instance I might have a \'billing address\' and \'shipping address\' and I dont wan

7条回答
  •  醉话见心
    2020-12-30 10:54

    For the more complex cases I moved away from simple DataAnnotations to the following: Validation with visitors and extension methods.

    If you want to make use of your DataAnnotations you would replace something like the following:

    public IEnumerable BrokenRules (Payment payment)
    {   
        // snip... 
        if (string.IsNullOrEmpty (payment.CCName))
        {
          yield return new ErrorInfo ("CCName", "Credit card name is required");
        }
    }
    

    with a method to validate a property by name via DataAnnotations (which I don't have atm).

提交回复
热议问题