Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Practices for MVC, jQuery and Handling Errors
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>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). </p> <p>So:</p> <p>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?</p> <p><strong>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.</strong> Because of the return type I may want a <em>different</em> result depending on the caller. Is there anyway to do this without having two Action Methods? </p> <p>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? </p> <p><strong>Edit:</strong> 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)</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload