asp.net mvc - Remote validation not working for MVC -


i have made remote validation in project, avoid duplicate entries in db. model class this

    public class supplier {     public int supplierid { get; set; }     public string suppliername { get; set; }     [required, displayname("supplier code")]     [remote("vicodeexists", "supplier", "vi code exists.", additionalfields = "supplierid")]     public string suppliercode { get; set; }  } 

and inside suppliercontroller have function this

 public jsonresult vicodeexists(string suppliercode, int supplierid = 0)     {         var user = _db.suppliers.where(x => x.suppliercode == suppliercode.trim() && x.supplierid != supplierid);         return !user.any() ?            json(true, jsonrequestbehavior.allowget) :            json(string.format("{0} exists.", suppliercode),                jsonrequestbehavior.allowget);     } 

in create view

 @html.textboxfor(model => model.suppliercode)  @html.validationmessagefor(model => model.suppliercode) 

everything looks okay me, validation not works. have tried adding breakpoint inside controller, never hit. can 1 point out doing wrong here?

note: have same type of validation in other controllers in same project , work well. issue 1 only.

you using overload of remoteattribute accepts 3 string parameters 3rd parameter area name (not error message).

change attribute to

[remote("vicodeexists", "supplier", errormessage = "vi code exists.", additionalfields = "supplierid")] public string suppliercode { get; set; } 

note overriding error message in methods return statement anyway, can omit , use

[remote("vicodeexists", "supplier", additionalfields = "supplierid")] public string suppliercode { get; set; } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -