Let's say we have a standard Ajax form with the following Syntax:
@using(Ajax.BeginForm("AddCityJson", "City",new AjaxOptions
{
HttpMethod = "post",
OnSuccess = "JScriptOnSuccess();",
OnFailure = "JScriptOnFailure"
}))
{
...form fields go here...
}
A simple form that gathers some information about a city, submits and saves it.
On the controller side we receive the info. If everything went well we return true in order for OnSuccess AjaxOption to trigger and if failed false is returned:
public JsonResult AddCityJson(Formcollection collection)
{
var city = new City
{
...populate city fields from passed in collection
}
var cityRepository = new CityRepository;
city = cityRepository.SaveOrEdit(city);
if(city==null)
{return Json(false)}
return Json(true);
}
Inside of the controller AddCityJson method I need to add various checks. Possibly to check if the city name already exists or any other validation and if I ran into an Error or a Warning return it back to UI.
How can I pass any error/warning messages up to UI if my Ajax form expects to get back ajax true or false and whether that post was successful?
I would like to avoid using ViewData, ViewBags. Thank you.