asp.net mvc - Access Controller context in custom ValidationAttribute -
i'm implementing custom validator using validationattribute:
public class customattribute : validationattribute { protected override validationresult isvalid (object value, validationcontext validationcontext) { //... } }
but run validation need access variable part of application's base of operations controller (it's related user logged in). how can hands on it?
if can populate model property variable can this:
model:
[customattribute("myvariableasmodelproperty", "failed validation!") public string validatethis{get; set;} public string myvariableasmodelproperty{get; set;}
your custom validator:
public class customattribute : validationattribute { string otherpropertyname; public customattribute(string otherpropertyname, string errormessage) : base(errormessage) { this.otherpropertyname = otherpropertyname; } protected override validationresult isvalid (object value, validationcontext validationcontext) { var otherpropertyinfo = validationcontext.objecttype.getproperty(this.otherpropertyname); var referenceproperty = (string)otherpropertyinfo.getvalue(validationcontext.objectinstance, null); //... }
now have 2 values compare: value
model property validated, , referenceproperty
variable.
asp.net-mvc validation asp.net-mvc-4
No comments:
Post a Comment