简体   繁体   中英

What would cause a MVC4 controller to only return a status code and no partial view?

This is sort of an odd question but then I have a very odd situation. On my development server (IIS & IIS Express) I make a ajax request and return a form. The user then posts the form back via an ajax post and if there are any validation errors the controller sends back the partial view with a response code of 400. In my ajax method I intercept any 400 errors and redisplay the form with the validation errors.

My problem is that when I upload my app to my production server all I get on a validation error is the 400 response with no partial view. I don't know even where to begin? Here is what I have tried, what libraries I am using, and some sample code.

ASP.net MVC4, Fluent Validation, jQuery, unobtrusive validation, malsup-ajaxsubmit

On my production server...

  1. My partial view that loads the form works as expected. This tells me that the application is having no problem finding my view as the same view is redisplayed if validation fails.
  2. My controller using fluent validation is correctly detecting a model state error and responding with a 400 (just no view with it).
  3. My ajax post using ajaxsubmit is posting to the server correctly using ajax.
  4. Using firebug I step through my ajax method and an 400 error is indeed returned but the only content on xhr.responseText is "Bad Request". NO partial view. Again my development machine works perfectly.

Here is some code.

My Form Get Request:

public ActionResult CreateMedicalEvent(int userId)
{
    var model = new EventModel { MedicalEventUserId = userId };

    return PartialView("_Event.Create", model);
}

My Form Post:

[HttpPost]
        public ActionResult CreateMedicalEvent(EventModel model)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = 400;
                return PartialView("_Event.Create", model);//this does not get returned on production server
            }

            //the rest of my code if validation passess
        }

My ajax method:

 $('.se-form').ajaxForm({
        delegation: true,
        beforeSubmit: function() {
            ajaxHelpers.modalProcess();//modal progress bar
        },
        success: function(data) {
            $.modal.close();
            //handle a successful post
        },
        error: function(xhr) {
            $.modal.close();
            if (xhr.status == 400) {
                slideEdit.seObj.empty();//this is my form object
                slideEdit.seObj.append(xhr.responseText);//all I get is "Bad Request" from production server
                $.validator.unobtrusive.parse($('form', slideEdit.seObj));
                ajaxHelpers.bindDates(); //because we need to bind event AND format date, we have to do this here
                utilities.formatDecimal();
            } else {
                ajaxHelpers.exc(xhr);
            }
        }
    });

I wish there was more code to post but this isn't all that complicated. Am I missing some weird dll file on my production server? Both servers have MVC4 installed. Lost. Pleas help. Thanks.

Adding to @STW 's answer, try adding to your web.config

<system.webServer>

  <httpErrors errorMode=”Detailed” />

</system.webserver>

to see the IIS error response.

Also see these links for more

Understanding 400 Bad Request Exception and ASPNET MVC IIS7 and Bad Request

A 400 indicates a bad request, meaning MVC didn't think it could handle the request being sent--the controller action won't even be invoked in this case. Try enabling detailed error output, or running the request against the site in Visual Studio and see if you can get the exception details. It could be happening in Routing, Controller construction, Action Invoking, or maybe Model Binding.

In my case, I accidentally had the PartialView.cshtml file in the wrong View directory. Moved it over to the right View directory and no more 400 bad request. Doh!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM