c# - Validation ErrorMessage value -
how emit custom validator error messages entity this?:
receipts exeeded invoice amount of 15000
my property
[invoiceamountnotexeeded(errormessage = "receipts exeeded invoice amount of {0}")] public int amount {get; set; } in validator:
var errormsg = formaterrormessage(string.format(validationcontext.displayname,invoice.amount)) problem m getting: receipts exeeded invoice amount of amount. note how writting property name instead of property value. advice?
edit: code added
public class invoiceamountnotexeededattribute : validationattribute { public invoiceamountnotexeededattribute() { } protected override validationresult isvalid(object value, validationcontext validationcontext) { var factid = ....; var db = new entities(); var fact = db.invoices.find(factid); var amountrecibos = ...; var amount = convert.toint32(value); if (amountrecibos + amount > fact.amount ){ var errormsg = formaterrormessage(string.format(validationcontext.displayname,invoice.amount)); return new validationresult(errormsg); } return validationresult.success; } }
the reason have behaviour because refer validationcontext.displayname default set property name ("amount" in case). string.format(validationcontext.displayname,invoice.amount) returns "amount". instead of try apply this:
var errormsg = formaterrormessage(invoice.amount.tostring()); return new validationresult(errormsg); this way pass formaterromessage not displayname property amount value instead , formaterrormessage use pattern errormessage attribute property. should give want.
Comments
Post a Comment