0

Have a web api doing crud, and a mvc front end. In front end controller, this is the edit code for jqgrid:

    [HttpPost]
    public JsonResult EditRecipientActivity(RecipientActivity model)
    {
        var result = HttpStatusCode.BadRequest;
        if (ModelState.IsValid)
        {
            if (model.ActCount > 0)
            {
                var response = repo.SaveUpdatedRecipientActivity(model);
                if (!response.IsSuccessStatusCode)
                {
                    result = HttpStatusCode.BadRequest;
                }
                else
                {
                    result = HttpStatusCode.OK;
                }

                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var response = repo.AddRecipientActivity(model);
                return Json(response.IsSuccessStatusCode, JsonRequestBehavior.AllowGet);
            }
        }
        return Json(false, JsonRequestBehavior.AllowGet);
    }

I am trying to pass the server error from the web api through to the jqgrid, so the message appears on the edit form.

With eh this code I can send just a status code to the form, but the actual status in the header is 200, even if the controller is returning a 400. How do I pass the actuall status code to the form?

EDIT: Changed the controller method. I can't use Request.CreateResponse, I'm guessing because this is not an API controller:

    [HttpPost]
    public HttpResponseMessage EditRecipientActivity(RecipientActivity model)
    {
        var response = repo.SaveUpdatedRecipientActivity(model);
        return response;
    }

This is the result sent to the web page:

StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache X-SourceFiles: =?UTF-8?B?QzpcX1ZTX1Byb2plY3RzXHRlc3RcU0ZBQVJfU1ZDXFNGQUFSX1NWQ1xyZWNpcGllbnRcdXBkYXRlYWN0aXZpdHlcMTIzMTIzMTIz?= Persistent-Auth: true Cache-Control: no-cache Date: Wed, 02 Sep 2015 16:44:13 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 2378 Content-Type: application/json; charset=utf-8 Expires: -1 }

This is what I was hoping, but it didn't trigger the jqGrid error message. Any idea why?

EDIT 2: Here is the navGrid code:

        $("#activity-grid").jqGrid('navGrid', '#grid-pager',
            { edit: true, add: true, del: false, search: true, refresh: true },
            { //edit parameters
                editData: { FV: fv, RecipKey: recipientId },
                beforeShowForm: function (form) { $("#tr_ActCount", form).hide(); }, // Hides the ActCount field from the modal form
                closeAfterEdit: true

            },
            { //add parameters
                editData: { FV: fv, ActCount: 0 },
                beforeShowForm: function (form) { $("#tr_ActCount", form).hide(); },  // Hides the ActCount field from the modal form
                closeAfterEdit: true
            }
BattlFrog
  • 3,370
  • 8
  • 56
  • 86

1 Answers1

1

One possible way would be to use HttpResponseMessage as the return type of EditRecipientActivity method and use Request.CreateResponse with HttpStatusCode.BadRequest or any other value as the first parameter of Request.CreateResponse. It would allow to set HTTP error code in the server response. The current code which you use just set numeric or boolean valu in the serve response.

Alternatively you can use throw new HttpResponseException(HttpStatusCode.BadRequest); (or some other HTTP error).

jqGrid provides you one more possibility, but it should be used only if the framework which you use would don't provide the possibility to set error HTTP code in the server response. In the case one can use afterSubmit callback of form editing. The callback get jqXHR as the first parameter and one can use responseText property to get the body of the server response (you returns currently the information about the error in the body of the server response). The callback afterSubmit should return [true] if the response from the server should be interpreted as successful and return [false, "error message"] in case of error.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg. I am now getting the response I expect, but it isn't triggering the error message in the form. See above edit. – BattlFrog Sep 02 '15 at 16:48
  • @BattlFrog: Sorry, but I don't see what information will be placed in **the body** of the `EditRecipientActivity` response now. The status code is now `500`, so the text from the body of the response, which could be HTML fragment in general, will be displayed in the edit form. If would be very helpful if you include the current JavaScript code which you use. If you use form editing defined in navigator bar then it would be enough to see the parameters of `navGrid` which you use. – Oleg Sep 02 '15 at 16:59
  • Ok, I have add the navGrid code. One things that is a bit unusual is I am using an MVC web front end, so the calls to the WEB API are not coming directly from the jqGrid code, they are going: jqGrid > MVC controller > WEB API Controller and back again. Not sure if this matters, but thought I would mention it. – BattlFrog Sep 02 '15 at 17:13
  • @BattlFrog: You can add `errorTextFormat: function (jqXHR) { alert("error: " + jqXHR.responseText); return "Error: BlaBla!" } ` callback in block `//edit parameters` and in `//add parameters`. The callback should be called. Using `jqXHR` you can access to body of the response, to status code or to any HTTP headers. So you can decode the error response to any form which you like to display in Edit form. Probably [the answer](http://stackoverflow.com/a/6803206/315935) will help you to understand more possibilities which you can use. – Oleg Sep 02 '15 at 18:23
  • I ended up using a variation of your code. I left the jqGrid as is, and switched returning an HttpStatusCodeResult: return new HttpStatusCodeResult(HttpStatusCode.OK, "Update Successful!"); Works like a charm. THanks for all your help, Oleg. – BattlFrog Sep 02 '15 at 20:44