Does anyone have a elegant way of dealing with errors in ASP.Net MVC? I constantly run into issues when dealing with requests to controller actions where the Action can be used for both normal requests and AJAX requests. The problem I have is finding an elegant way of dealing with these issues.
For example, how could I handle validation errors? Ideally I would like to submit the form to a server via AJAX and then return any errors the action threw and display them on the page, but for the same to work via a normal postback when the client has JavaScript turned off. I know I can use the jQuery Validation Plugin as they type but it's isn't the same, nor is it ideal considering the restrictions on the data to be validated would be specified in two places (my nHibernate Validation Mappings and in the JavaScript file).
How about when the user requests a non-existent record? Should I redirect to a 404 page? What if the request was made via Ajax (say, to load in a dialogue box).
So:
How do you handle errors thrown by controller actions when they were called using Ajax? Especially Model State errors (i.e validation). Can I send it via JSON?
Do you have tips on how to make controller actions which work well when called normally and via Ajax? This is a annoying problem when writing action methods. Because of the return type I may want a different result depending on the caller. Is there anyway to do this without having two Action Methods?
What is your general strategy for handling errors in actions on MVC? Do you redirect to error pages a lot? Do you redirect to another page?
Edit: I think part of the problem is I want different things to happen, so if there is an error I would want to stop any progress until it's fix and therefore send a error back. Otherwise I may want to update different areas of the page. But if I had one return how would I know it's a success or failure without wrapping it an object that has a property indicting so (thus making it more difficult to use partial views)
Thanks