C#C
C#3y ago
Shinigami

✅ ValidationContext in ASP.NET

What does ObjectInstance and ObjectType mean? afaik

1) ValidationContext is basically on which class the validation is performed.
2) ObjectInstance is where we have all the values for a model class
3) I'm not sure what object type is.

What does this code do here?

PropertyInfo? otherproperty = validationContext.ObjectType.GetProperty(PropertyName);

                    DateTime from_date = Convert.ToDateTime(otherproperty.GetValue(validationContext.ObjectInstance));

I'm trying to fetch a different property value from in the same model class

Model class

//Multiple Properties using Reflection
public string? FromDate { get; set; }

[DateRangeValidator("FromDate", ErrorMessage = "From Date should be less than To Date.")]
public string? ToDate { get; set; }
Was this page helpful?