I have a complex type that I return when I perform various operations.And I want to know how I can pass back a custom JSON object to show any errors. Consider the following code
Business Layer
public ResultObject StartJob(string jobName){
...
return new ResultObject{ErrorMessage = "Job cannot be started due to ..."};
}
Controller
[HttpPost]
public ActionResult StartJob(string jobName){
var resultObject = BusinessLayer.StartJob(jobName);
if (resultObject.HasErrors){
return Json(new {success = false, message = resultObject.message}, JsonRequestBehavior.AllowGet);
}
else{
return Json(new {success = true}, JsonRequestBeahvior.AllowGet);
}
}
When I perform the ajax post, despite me returning success = false, the ajax call is still successful and jQuery does not call error() method.
This type of pattern is repeated multiple times.
if(data.success){...}else{ myerror(data.message);}