I want to capture my error message in the data-ajax-failure
portion of my form which uses unobtrusive ajax. It's easy to do this if the form is posted via a function which calls $ajax
but I'm not doing that with this, I'm placing both my success and failure function calls in the form itself:
<form method="post" data-ajax-url="/Home/Process_Form" data-ajax="true" data-ajax-method="post" data-ajax-success="form_data_success" data-ajax-failure="form_data_fail">
The form post to a controller action called Process_Form
public IActionResult Process_Charterer(Charterer model) {
if (ModelState.IsValid)
{
bool safetyCheck = _chartererService.GetCharterers().Any(x => x.ChartererName.Contains(model.ChartererName));
if (safetyCheck == true)
{
//Return something to the ajax failure
}
else
{
try
{
_chartererService.InsertCharterer(model);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
return PartialView("_Window_Charterer");
}
The function should then capture the failure and do something with it, for example I'm going to show an alert for the user.
function form_data_fail(result) {
//capture the error from the controller and do something with it.
}
Can anyone explain how this is done with this particular setup?