c# - Validation attribute depending on object instance type -
i'm facing conception problem. create , update our deals in 2 ways : using web forms (one create deals, 1 edit them) , through integration file (to allow mass creation & update).
public class createdealviewmodel { public int dealid { get; set; } [validatesalesman] public int salesmanid { get; set; } } public class editdealviewmodel { public int dealid { get; set; } [validatesalesman] public int salesmanid { get; set; } } public class integrationline { public int dealid { get; set; } [validatesalesman] public int salesmanid { get; set; } public string status { get; set; } }
i have validation logic implement : @ deal creation, active salesman accepted ; in update, active salesman plus previous salesman value (stored in db) accepted.
i wrote :
public class validatesalesman : validationattribute { protected override validationresult isvalid(object value, validationcontext validationcontext) { var container = validationcontext.objectinstance; if (container.gettype() == typeof(integrationline)) { if(((integrationline)container).status == "creation") { //validation logic here } else { //validation logic here } } else if(container.gettype() == typeof(createdealviewmodel)) { //validation logic here } else if(container.gettype() == typeof(editdealviewmodel)) { //validation logic here } } }
}
is approach (mvc compliant) or not ? validation attribute has know witch kind of model applies on ?
thanks in advance advice :)
Comments
Post a Comment