i want a jqGrid class that can return json by mvc

2019-06-08 01:23发布

问题:

Using jqGrid in mvc razor

I want a class that can go for jqGrid json format and bind the grid.

回答1:

I think that you don't need to define any class to produce JSON data needed for jqGrid. You can return anonymous object:

public JsonResult DynamicGridData (string sidx, string sord, int page, int rows)
{
    var query = ...;
    var totalRecords = query.Count();

    return Json(new {
        total = (totalRecords + rows - 1) / rows,
        page,
        records = totalRecords,
        rows = (from item in query
                select new {
                    id = item.Id.ToString(),
                    cell = new[] {
                        item.FirstName,
                        item.LastName,
                        item.Votes.ToString(),
                        item.Title
                    }
                }).ToList()
    },
    JsonRequestBehavior.AllowGet);
}


回答2:

Firstly this is the wrong way of asking question at Stackoverflow.com please read Faq and whathaveyoutried.com

But since you are new to Stackoverflow I will answer this one for you,

public class JqGridModel<T>
{
    public int page { get; set; }
    public Int32? total { get; set; }
    public Double? records { get; set; }
    public IEnumerable<T> GridData { get; set; }

    public JqGridModel<T> Bind(IEnumerable<T> data)
    {
        records = data.Count();
        GridData = data;
        page = 1;

        return this;
    }
}

also from your comments you said,

actually i am trying to use jqgrid with mvc razor and entity framework.

I recommend you go through this article once, it might be helpful. Here too a jqGrid class is defined as... the above one is more generic though, but it all depends on your use case.

public class JqGridObject
{
    public string Page { get; set; }
    public int PageSize { get; set; }
    public string SortColumn { get; set; }
    public string SortOrder { get; set; }
    public List<Fruit> Data { get; set; }
}

public class Fruit
{
    public int Id { get; set; }
    public string Name { get; set; }
}