2

I am using ASP.NET MVC AJAX with the @AJAX.ActionLink helper to implement item deletion from a grid. When the request is executed it returns the grid without the deleted item and the AJAX helpers replace the grid. This works just fine but the problem is what to do if the item cannot be deleted (in my case due to foreign key constraints). How can I report the error to the client and display appropriate message. I don't want to use the OnFailure function blindly because I don't want to show the same error message for all errors. Ideally there will be a way to send specific error from the action and inspect the error in the JavaScript OnFailure function but I can't seem to find it.

Of course I can downgrade to returning JSON, manually executing AJAX calls and replacing UI elements but I don't really feel like it. I'd rather do something dirty like render a hidden input with the error information and check for it on the client but I don't want to do that either if there is better way.

1 Answer 1

2

You can return status code and error message from the controller and pass it to the ajax OnFailure function

return new HttpStatusCodeResult(HttpStatusCode.Conflict, "The entity is currently used");

as shown here: ASP.NET MVC AJAX onError handling

and in the client side JS:

function deleteFailure(responce) {
    var statusCode = responce.status;
    var responceText = responce.statusText;
    if(statusCode == 409){
        //do something specific for specific errors
    }
    alert(responceText);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.