I am trying to delete item from table. There is Ajax link for it.
@Ajax.ActionLink("Delete", "DeleteConfirm", new { id = Model.ID }, new AjaxOptions {
HttpMethod = "POST", UpdateTargetId = "TableID", OnSuccess = "CloseDialog", OnFailure = "AlerDialog"
})
It calls DeleteConfirm method from controller with POST method. I made simple controller which should do something so ActionLink should catch error and run OnFailure function (to show alert dialog).
Controller:
public ActionResult DeleteConfirm(int id)
{
// code here
}
What to return from controller method so OnFailure function invokes?
how about throwing an exception?
Your ajax call would then know it was a failure, and run the correct function.
Edit I have added a second exception type to satisfy the comments. I still feel this is a good answer, and that the down vote is unfair.
OnError is fired when error happens on serverside. By error, I mean exception, and I think you can't pass exception message on clientside except of 500 Server error. I think that good aproach is to have some CustomResponse class that your action will return. In your case, something like:
In DeleteConfirm action you create new response, which maybe needs to inheriteActionResult class(I'm not sure because I'm new to MVC). If some error ocures while deleting, set DeletionSuccesfull to false, and Message to message of exception, or some custom message.
On client side, the point is to examine success in OnSuccess handler, and then decide what to do. Something like:
The OnFailure will fire based off the status code of the result, so something like this would give the desired effect.
Also, not sure if it's related but shouldn't your OnFailure text be "AlertDialog" instead of "AlerDialog"?
EDIT: In your controller action you should be able to test that the request is being made via Ajax by using this Extension method MVC provides
Request.IsAjaxRequest()
. Note that there is no true way to check if a request is an Ajax request on the server, this method is utilizing the presence of a custom header jQuery sets for all ajax requests it makes, in other words don't useRequest.IsAjaxRequest()
in business logic.Source for IsAjaxRequest() method