'Unable to get id error value' of jqGrid property

I'm trying to integrate jqGrid into an ASP.NET MVC 4 Razor view and fall into the trap that puzzled me. When I try to get into the view in debug mode using IE9, I get this Javascript error:

Microsoft JScript runtime error: Unable to get value of the property 'id': object is null or undefined.

Razor:

<link href="/Content/themes/ui-lightness/jquery-ui-1.8.19.custom.css" rel="stylesheet" type="text/css" />
<link href="/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="~/Scripts/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>
@using(Html.BeginForm())
{
    <fieldset>
        <legend>@Model.FirstName @Model.LastName - Contacts/Providers</legend>
        <table id="clientContacts" cellpadding="0" cellspacing="0"></table>
    </fieldset>
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#clientContacts").jqGrid({
            url: '@Url.Action("GetClientContactsAndProviders")',
            datatype: 'json',
            mtype: 'POST',
            colNames: ['clientContactId', 'Type', 'Who', 'Phone', 'button', 'Comments'],
            colModel: [
                { name: 'clientContactId', index: 'clientContactId', hidden: true },
                { name: 'contactType', index: 'Type', width: 190, align: 'left' },
                { name: 'contactName', index: 'Who', width: 200, align: 'left' },
                { name: 'contactPhone', index: 'Phone', width: 100, align: 'left' },
                { name: 'newContactButton', index: 'button', width: 50, align: 'center' },
                { name: 'contactComments', index: 'Comments', width: 240, align: 'left'}
            ],
            rowNum: 20,
            width: 780,
            height: '100%'
        });
    });
</script>

Act:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetClientContactsAndProviders(string sidx, string sord, int page, int rows)
{
    var clientId = CookieHelper.GetClientIdCookieValue();
    var client = _clientRepo.GetClient(clientId);
    var contacts = client.Contacts.ToList();
    var model = new
        {
            total = 1,
            page = 1,
            records = contacts.Count,
            rows = contacts.Select(c =>
                new
                    {
                        clientContactId = c.ClientContactId.ToString(),
                        contactType = c.ContactType.Description,
                        contactName =
                            c.Contact.Person.FirstName + " " + c.Contact.Person.LastName,
                        contactPhone = c.Contact.Person.Phone1 ?? string.Empty,
                        newContactButton = string.Empty,
                        contactComments = c.Comments ?? string.Empty
                    }).ToArray()
        };
    return Json(model);
}

I don’t get this error in Chrome, and I can’t understand all my life what this mythical property “id” is. Please, help!

Edit (after @Justin Ethier's answer): Full disclosure, I didn’t think it mattered, but I am trying to go through four “empty” lines in this situation. In these cases, clientContactIdthe default value is 0, and I think this could be part of the problem. I changed my Javascript in the grid as follows:

$(document).ready(function () {
    $("#clientContacts").jqGrid({
        url: '@Url.Action("GetClientContactsAndProviders")',
        datatype: 'json',
        mtype: 'POST',
        colNames: ['clientContactId', 'Type', 'Who', 'Phone', 'button', 'Comments'],
        colModel: [
            { name: 'clientContactId', index: 'clientContactId', hidden: true },
            { name: 'contactType', index: 'Type', width: 190, align: 'left' },
            { name: 'contactName', index: 'Who', width: 200, align: 'left' },
            { name: 'contactPhone', index: 'Phone', width: 100, align: 'left' },
            { name: 'newContactButton', index: 'button', width: 50, align: 'center' },
            { name: 'contactComments', index: 'Comments', width: 240, align: 'left'}
        ],
        rowNum: 20,
        width: 780,
        height: '100%',
        jsonReader: {
            root: 'rows',
            page: 'page',
            total: 'total',
            records: 'records',
            repeatitems: true,
            cell: 'cell',
            id: 'clientContactId',
            userdata: 'userdata',
            subgrid: { root: 'rows', repeatitems: true, cell: 'cell' }
        }            
    });
});

, , "", rows, jqGrid:

var rowBuilder = new List<object>();
for(var i = 0;i < contacts.Count; i++)
{
    rowBuilder.Add(new
                       {
                           id = i + 1,
                           cell = new
                                      {
                                          clientContactId = contacts[i].ClientContactId.ToString(),
                                          contactType = contacts[i].ContactType.Description,
                                          contactName =
                       contacts[i].Contact.Person.FirstName + " " + contacts[i].Contact.Person.LastName,
                                          contactPhone = contacts[i].Contact.Person.Phone1 ?? string.Empty,
                                          button = string.Empty,
                                          contactComments = contacts[i].Comments ?? string.Empty
                                      }
                       });
}
var model = new
                {
                    total = 1,
                    page = 1,
                    records = contacts.Count,
                    rows = rowBuilder.ToArray()
                };
return Json(model);

!

+3
2

jqGrid jsonReader json-. , jqGrid:

{ 
  "total": "xxx", 
  "page": "yyy", 
  "records": "zzz",
  "rows" : [
    {"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
    {"id" :"2", "cell":["cell21", "cell22", "cell23"]},
      ...
  ]
}

, id, , id, , . , , :

jsonReader : {
    root: "rows",
    page: "page",
    total: "total",
    records: "records",
    repeatitems: true,
    cell: "cell",
    id: "clientContactId",
    userdata: "userdata",
    subgrid: {root:"rows", 
    repeatitems: true, 
    cell:"cell"
    }
},

?

+4

GET firefox, chrome, ...

Trow.cells[0].childNodes[0].nodeValue ,

Trow.cells[0].innerText firefox chrome....

0

All Articles