0

I am writing an MVC3 application and on one of the pages I bring back a list of items that are displayed in a table with paging and sorting.

However, I want to put up a message telling the user to refine their search further if the records bought back are over a certain number.

I have implemented this by changing the Response.StatusCode in my controller, if the above condition is met;

public PartialViewResult VerbatimGridUpdate(VerbatimFormModel model)
{
    if (ModelState.IsValid)
    {
        var mod = ModelBuilder.GetVerbatimFormModel(model);

        if (mod.Verbatims.Count() > 10000)
        {
            HttpContext.Response.StatusCode = 33;
        }

        return PartialView("_VerbatimGrid", mod);
    }
    else
    {
        return PartialView(model.Verbatims); 
    }
}

And in my markup I capture this failure using the OnFailure event of the Ajax form;

    function FailureLoading(ajaxContext) {
        if (ajaxContext.status == "33") {
            var ul = $("#validationSummary ul");
            $("ul").empty();
            ul.append("<li>" + "Too many records returned, please refine your search." + "</li>");
        }
    }

</script>

<div class="filters" >
    @using (Ajax.BeginForm("VerbatimGridUpdate", null, new AjaxOptions { HttpMethod= "Get", UpdateTargetId = "grid-container",  OnBegin = "StartLoading", OnSuccess = "FinishLoading", OnFailure="FailureLoading"}, new { id = "VerbatimListForm", name="VerbatimListForm" }))
    { 

everything works fine locally but as soon as I deploy to our UAT server, the failure event isn't fired and all the records are displayed. I can't work out why I am getting this difference in behaviour. Any ideas will be most welcomed. Thank you.

saj
  • 4,626
  • 2
  • 26
  • 25
  • What is the status code returned when you inspect the HTTP response? 33 or something else? – avesse Jun 11 '12 at 16:17
  • The status code returned is 200 – saj Jun 12 '12 at 07:40
  • That's probably IIS overriding your status code then. See http://stackoverflow.com/questions/434272/iis7-overrides-customerrors-when-setting-response-statuscode (but I agree with Mark's answer below; an HTTP status code is the wrong method to use here). – avesse Jun 12 '12 at 12:35

2 Answers2

1

I should not use custom http status code for that purpose though i'm not sure it's right or not. First of all it's not an error all you want to do is convey the user there are more records out there (information).

You could have use a hidden field in the partial view that says this status and in the onSuccess method you have to read the hidden field from the partial view and based upon the status update the information to user.

VJAI
  • 32,167
  • 23
  • 102
  • 164
0

This could be happening for many reasons, some common ones are:

  • Different domain between client and server.

AJAX calls by default do not allow cross-domain access. You can view some solutions for this problem here.

  • Relative vs. absolute path.

Sometimes you development server will be something like http://localhost/myController/myAction and then your UAT will be something like http://myUatserver/myApp/myController/myAction.

If you are using absolute root paths like /myController/myAction it will go to the host root and not find the URL. Try to explicitly include the controller & action in the Ajax.BeginForm().

You can find out for sure where the Ajax call is being made by using a debugger like Firebug or Chrome's dev tools and look for the XHR calls.

Terry
  • 14,099
  • 9
  • 56
  • 84