How to call ValidationAttributes manually? (DataAnnotations and ModelState)

后端 未结 2 1152
滥情空心
滥情空心 2021-01-05 16:56

We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataanno

相关标签:
2条回答
  • 2021-01-05 17:26

    You do use the System.ComponentModel.DataAnnotations.Validator class to validate objects.

    0 讨论(0)
  • 2021-01-05 17:32

    This is because you are passing the source object to the Validate method, instead of the property value. The following is more likely to work as expected (though obviously not for indexed properties):

    attrib.Validate(propertyInfo.GetValue(obj, null), propertyInfo.Name);
    

    You would certainly have an easier time using the Validator class as Steven suggested, though.

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