I have a form with some input fields and when you click on Save, I do a check if a field already exist in the database. I have a service method for this.
For example in the database the field with value "Test10" already exist and if the user use "Test10" in the input field and clicks on save I want to show this message :
private async Task<bool> CheckIfCodeAlreadyExist(string code)
{
return await _service.CheckCodeExist(code);
}
I tried with session vaiables, with an extra bool parameter but not effective enough. My JavaScript knowlegde is not that much, but would it possible to check this with a "onClick" event in the form?
If the result is true you stay on the page with the message.
Don't mind the Model.Code is red.
You don't need to do it with onClick if you have liberty to use
Remote
validation .net MVC offers.Decorate you model property with something like:
[Remote("Action", "Controller", ErrorMessage = "Invalid Code")]
Or pass additional fields as well:
[Remote("Action", "Controller", AdditionalFields = "Id", ErrorMessage = "Invalid Code")]
with javascript and jquery, you do ajax request.
Bind click event to button:
Controller/Action to return bool: