user1108948
user1108948

Reputation:

No element found when delete a record

JqGrid 4.6.

Everything works fine. The only thing is that when I open the Firefox debugger and go to the console. If I delete a record(click the trash icon, then the delete dialog pops out, click Delete button and the page is refreshed etc), the debugger warns me.

no element found

The possible scripts are:

$(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;
                }
            }

Also

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>');
        }

Although the error has not impacted my result. I can't locate the warning. I want remove it.

EDIT:

I tried it in google chrome. It seems okay. Maybe it is the bug in Firefox?

Upvotes: 1

Views: 234

Answers (1)

Oleg
Oleg

Reputation: 221997

After creating the demo project which can be used to reproduce "the problem" I can examine and describe it.

To reproduce the problem one need to start the MVC application and to use Firefox as frontend. One should start the integrated Debugger (by Ctrl+Shift+S or menu "Tools" / "Web Developer" / "Debugger") and to examine Browser Console window. The window contains many warnings, which are suspected for Firefox, but what are absolutely correct actions and the warnings are absolutely unneeded. After deleting of any row one will see the message like

enter image description here

I examined the problem exactly and it's really wrong warning, because of misinterpretation of HTTP traffic of REST operation. The DELETE method of ASP.NET MVC, which have void as return value (like public void DeleteProduct(int id)) produces the HTTP response like

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

The bug of Firefox: it displays message "no element found" for all HTTP responses which have no body. Thus if the status code is 204 (No Content) or if the status code is 200 (OK), but the body is empty (there are exist HTTP header Content-Length: 0) then Firefox suspect that the REST resource is not found and it display the "warning" with the text "no element found".

If you don't want to see the message then you have to return some data in the body of DELETE response. For example

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

which produces the response like

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!"

I personally think that one should better just ignore "the warning" of Firefox and to hold public HttpResponseMessage DeleteProduct(int id). I would still recommend you to update the repository which you use to

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

where Remove have Boolean as return type. The implementation can be

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

and the MVC code

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

will be fixed to

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

I want to stress that all above problem is pure ASP.NET MVC problem or a problem of Firefox and it have no direct relation to free jqGrid or jqGrid.

You can download the modified project 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).

Upvotes: 2

Related Questions