user1447718
user1447718

Reputation: 681

JQGrid not displaying JSON data when getting JSON data via AJAX

Mine is a MVC project. From my view am making a AJAX call to get the JSON data. From my controller when i deseralize my class using javascriptDeserializer, i get below

{
    "page":1,
    "total":2,
    "records":2,
    "rows":[
        {"id":1,"cell":["ToolVersion","1","ESP","1.2","2/2/2013"]},
        {"id":2,"cell":["ToolVersion","2","OPT","1.3","3/3/2013"]}
    ]
}

The action method as below

var lst = obj.GetDetails(); // this returns the instance of MyCustomJQGrid class.
return Json(new
{
    total = 2,
    page = 1,
    records = 2,
    rows = lst.rows.ToArray()
}, JsonRequestBehavior.AllowGet);

public class MyCustoMJQGrid
{
    public class Row
    {
        public int id { get; set; }
        public List<string> cell { get; set; }
        public Row()
        {
            cell = new List<string>();
        }
    }

    public int page { get; set; }
    public int total { get; set; }
    public int records { get; set; }
    public List<Row> rows { get; set; }
    public JQGrid()
    {
        rows = new List<Row>();
    }
}

in view, am using below code

$('#list2').jqGrid({
        url: '@(Url.Action("GetToolVersionDetails", "Admin"))',
        datatype: 'json',
        colNames: ['TableName','RowId', 'ColA', 'ColB', 'ColC'],
        mtype: 'GET',
        colModel: [
                    { name: 'TableName', index: 'TableName', width: 150 },
                    { name: 'RowId', index: 'RowId', width: 150 },
                    { name: 'ColA', index: 'ColA', width: 250 },
                    { name: 'ColB', index: 'ColB', width: 250 },
                    { name: 'ColC', index: 'ColC', width: 250 }
        ],
        jsonReader: {
            root: 'rows',
            total: 'total',
            page: 'page',
            records: 'records',
            cell: 'cell',
            id: 'id',
            repeatitems: false
        },
        rowNum: 10,
        rowList: [5, 10, 20, 30],
        pager: $('#gridpager'),
        sortname: 'RowId',
        viewrecords: true,
        sortorder: 'asc',
        caption: 'Tool Version',
        shrinkToFit: true,
        width: $('#gridContainer').width(),
        height: 200,
        hidegrid: false,
    });

jqgrid is getting displayed with two empty rows. no data is displayed. it just shows a blank table with two rows. JSON data is not getting displayed.

Kindly help.

Thanks.

Upvotes: 1

Views: 2504

Answers (1)

Amin Saqi
Amin Saqi

Reputation: 18977

I suggest you to install and use MvcJqGrid nuget package, to have a full featured fluent grid solution with mvc helpers.

It represents a repository class to select, filter and sort rows. It also represents a GridSettings type that you can simply use in your controller to put data rows into a json response and send it to the view.

Examples are here.

Codes for repository class are here. You also can find all other required codes there

Upvotes: 1

Related Questions