I am updating an MVC2 application so that it works in the new MVC4 paradigm. I am trying to figure out how one would call a method to validate an input field with unobtrusive ajax? I need to figure out if a certain value is unique in the database (wrapped by a service). I already have the service methods mapped out, I just need to be able to call it... Normally this is what I would do.
HospitalFinNumber: {
required: function (element) {
debugger;
return '@isFlagSet' != 'True'; //$('AddEnc').val() === 'AddEnc';
},
minlength: 6,
remote: function () {
//debugger;
return {
url: '@Url.Action("ValidateHosFin", "EditEncounter")',
data: { hospitalFin: $('#HospitalFinNumber').val(), encflag: '@encflag' }
};
}
}
But how would I do this now? I know my other functions don't work with unobtrusive ajax and validation? Is there anyway that I can just call these functions that I knew worked in the past with this methodology? UPDATE
I just realized you could pepper a field with [Remote(MethodName, Controller, ErrorMessage)]. But I just remember that I call this remote method conditionally. I use the same view to either edit or add a new row into the database... If I am just editing the Row, I do not check to see if the field is unique. If I am adding I do check to see if the field is unique. I figure out whether or not I am adding or edditing by having the controller set a tempData variable on the view... You can see it in the above example... the field is only required if that variable is set. I then remote to check if the value entered was unique or not... How can I get similar functionality using unobtrusive ajax?
ENDUPDATE Thanks.
You need to use jQuery.validate.unobtrusive to hook your standard validation rules, for more complex, custom rules, you can create adapters that make the ajax calls or whatever your validation requires