删除记录时找不到元素

No element found when delete a record

JqGrid 4.6.

一切正常。唯一的问题是当我打开 Firefox 调试器并转到控制台时。如果我删除一条记录(点击垃圾桶图标,然后弹出删除对话框,点击删除按钮,页面刷新等),调试器会警告我。

no element found

可能的脚本是:

$(gridSelector).jqGrid('navGrid', pagerSelector,
            {
                //navbar options
                edit: true,
                editicon: 'ace-icon fa fa-pencil blue',
                add: true,
                addicon: 'ace-icon fa fa-plus-circle purple',
                del: true,
                delicon: 'ace-icon fa fa-trash-o red',
                search: true,
                searchicon: 'ace-icon fa fa-search orange',
                refresh: true,
                refreshicon: 'ace-icon fa fa-refresh green',
                view: true,
                viewicon: 'ace-icon fa fa-search-plus grey',
                beforeRefresh: function () {
                    grid.jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');
                }
            },

            {
                //delete record form
                closeAfterDelete: true,
                recreateForm: true,
                mtype: 'DELETE',
                onclickSubmit: function (params, postdata) {
                    params.url = API_URL + 'DeleteVendor';
                },
                beforeShowForm: function (e) {
                    var form = $(e[0]);
                    if (form.data('styled')) return false;

                    form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />');
                    styleDeleteForm(form);

                    form.data('styled', true);
                    return true;
                }
            }

还有

function styleDeleteForm(form) {
            var buttons = form.next().find('.EditButton .fm-button');
            buttons.addClass('btn btn-sm btn-white btn-round').find('[class*="-icon"]').hide(); //ui-icon, s-icon
            buttons.eq(0).addClass('btn-danger').prepend('<i class="ace-icon fa fa-trash-o"></i>');
            buttons.eq(1).addClass('btn-default').prepend('<i class="ace-icon fa fa-times"></i>');
        }

虽然这个错误并没有影响我的结果。我找不到警告。我想删除它。

编辑:

我在googlechrome试过了。好像还可以也许这是 Firefox 中的错误?

创建可用于复制的 the demo project 后 "the problem" 我可以检查和描述它。

要重现问题,需要启动 MVC 应用程序并使用 Firefox 作为前端。应该启动集成调试器(通过 Ctrl+Shift+S 或菜单 "Tools" / "Web Developer" / "Debugger")并检查浏览器控制台 window。 window里面有很多警告,怀疑是Firefox的,但是什么都是绝对正确的操作,警告是绝对不需要的。删除任何一行后,将看到类似

的消息

我仔细检查了问题,确实是错误警告,因为对 REST 操作的 HTTP 流量的误解。 ASP.NET MVC 的 DELETE 方法,其中 void 作为 return 值(如 public void DeleteProduct(int id))产生像

这样的 HTTP 响应
HTTP/1.1 204 No Content
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNA==?=
X-Powered-By: ASP.NET
Date: Fri, 12 Feb 2016 09:23:51 GMT

Firefox 的错误:它为所有没有 body 的 HTTP 响应显示消息 "no element found"。因此,如果状态代码为 204(无内容)或状态代码为 200(正常),但 body 为空(存在 HTTP header Content-Length: 0) 然后 Firefox 怀疑未找到 REST 资源并显示 "warning" 和文本 "no element found".

如果您不想看到该消息,则必须 return 在 body 的 DELETE 响应中删除一些数据。例如

public HttpResponseMessage DeleteProduct(int id)
{
    bool isDeleted = _repository.Remove(id);
    if (!isDeleted) {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
    return Request.CreateResponse(HttpStatusCode.OK, "OK!");
}

产生类似

的响应
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNg==?=
X-Powered-By: ASP.NET
Date: Fri, 12 Feb 2016 09:05:19 GMT
Content-Length: 5

"OK!"

我个人认为最好忽略 Firefox "the warning" 并保持 public HttpResponseMessage DeleteProduct(int id)。我仍然会建议您将您使用的存储库更新为

interface IProductRepository
{
    IEnumerable<Product> GetAll();
    Product Get(int id);
    Product Add(Product item);
    bool Remove(int id);
    bool Update(Product item);
}

其中 Remove 的布尔类型为 return。实现可以

public bool Remove(int id)
{
    return _products.RemoveAll(p => p.Id == id) > 0;
}

和 MVC 代码

public void DeleteProduct(int id)
{
    _repository.Remove(id);
}

将固定为

public void DeleteProduct(int id)
{
    bool isDeleted = _repository.Remove(id);
    if (!isDeleted)
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
}

我想强调以上所有问题都是纯粹的 ASP.NET MVC 问题或 Firefox 的问题,与免费的 jqGrid 或 jqGrid 没有直接关系。

您可以下载修改后的项目here. The ProductsController.cs file contains commented version of DeleteProduct, which don't produce any warning in Firefox. You can play with the code by changing the dummy text "OK!" to the empty string "" or some other tests. The Firefox bug is very old (it's origin seams be the Bug 521301).