我将http错误返回给JQGrid,但它看到了200个代码

时间:2015-09-01 20:56:27

标签: asp.net-mvc jqgrid http-status-codes

有一个web api做crud和一个mvc前端。在前端控制器中,这是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);
    }

我正在尝试将服务器错误从web api传递到jqgrid,因此消息会显示在编辑表单上。

使用这段代码我只能向表单发送一个状态代码,但是标题中的实际状态是200,即使控制器返回400.如何将执行状态代码传递给表单? / p>

编辑:更改了控制器方法。我无法使用Request.CreateResponse,我猜是因为这不是API控制器:

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

这是发送到网页的结果:

StatusCode:500,ReasonPhrase:'内部服务器错误',版本:1.1,内容:System.Net.Http.StreamContent,标头: {   Pragma:没有缓存   X-SourceFiles:=?UTF-8?B?QzpcX1ZTX1Byb2plY3RzXHRlc3RcU0ZBQVJfU1ZDXFNGQUFSX1NWQ1xyZWNpcGllbnRcdXBkYXRlYWN0aXZpdHlcMTIzMTIzMTIz?=   Persistent-Auth:是的   缓存控制:无缓存   日期:2015年9月2日星期三16:44:13 GMT   服务器:Microsoft-IIS / 8.0   X-AspNet-Version:4.0.30319   X-Powered-By:ASP.NET   内容长度:2378   Content-Type:application / json;字符集= utf-8的   到期:-1 }

这是我所希望的,但它没有触发jqGrid错误消息。知道为什么吗?

编辑2:这是navGrid代码:

        $("#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
            }

1 个答案:

答案 0 :(得分: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.