views:

39

answers:

1

I am using Ajax.BeginForm(... in asp.net MVC 2.0
I would like to return HTTP status codes from my controller.
For example:

  • 400 (validation error)
  • 200 (if everything is OK)

I would like to use new AjaxOptions{UpdateTargetID="ajax-form", OnSuccess="...", OnFailure="..."} to handle these scenarios, however If I return a statuscode of 400 the UpdateTarget does not update my html.

Is there a way to have this update occur for "failures" as well as successes?

+1  A: 

Typically, servers don't return meaningful content for 4** errors. When they do, browsers don't tend to bother to display it. So, no, I don't think MVC is being overly finicky here.

If you think 400 is the right response from your server, then your choices are to use $.ajax() instead of Html.BeginForm or to handle OnFailure.

Craig Stuntz
Yes, I am using the http status code to convey meaning from the server to the client (this is their intent after-all) ... How would I handle OnFailure to show the HTML ?
Myster
Something along the lines of: `<% using (Ajax.BeginForm("JsonTest", new AjaxOptions() {OnFailure = "function(data) { $("#ajax-form").html(data.get_response().get_responseData());" })) { %>` You'll have to fiddle with this; I generally use the `$.ajax` method myself.
Craig Stuntz
ahh, excellent, that's the ticket :-)
Myster