0

I have a class "SampleDTO.cs" and i would like assign data annanatoation attributes during run time.

For an example

SampleDTO.cs {

public string Name
{
    get;set;

}

}

I need to force, required field validation, minimum or maximum field validation based on condition. I have been searching for a forum and couldnt get any solution..

My expection is,

if(some condition1)
{
    check SampleDTO.Name property for Required field validator check.
}
else if(some condition2)
{
    check SampleDTO.Name property for minimum and maximum length check.
}
else{
    someother check.
}

(This can be done easily, by decorating the required, min/max length attribute on the SampleDTO.cs class. I am expecting this to implement it dynamically instead of static assignment.)

Thanks in advance.

kamal
  • 9
  • 5
  • Is the condition linked to the DTO itself, or is it external to the DTO? Put another way, can you give an example condition? – Kirk Larkin Sep 05 '17 at 20:01
  • Seems like there is a better way to go about what you want, but this may be possible, depending on exactly what you need, by customizing DataAnnotationsModelValidatorProvider. See: https://stackoverflow.com/questions/4088274/possible-to-change-data-annotations-during-runtime-asp-net-mvcs-range-requ – stephen.vakil Sep 05 '17 at 20:13

0 Answers0