开发者

problem with jqgrid in jequery in asp.net mvc

I am developing the ASP.NET MVC 2 application . I want to show the gird of list of object data. like List<MyObject>. So I tried a code in controller action as:

[HttpGet]
public ActionResult JsonSalesCollection()
{
    string id = Request.QueryString["id"];

    ViewData["TrustId"] = id;
    Guid id1 = new Guid(id);

    List<TrustContract> contractList = trustContractmang.GetListOfTrustContractByTrustId(id1);
    int pageIndex = 12;
    int pageSize = 20;
    int totalRecords = contractList.Count();
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
    string orderBy = string.Format("{0} {1}", "TrustContracId", "desc");

    var jsonData = new
    {
        total = totalPages,
        page = 20,
        records = totalRecords,
        rows = (
            from s in contractList
            select new
            {
                i = s.TrustContracId,
                cell = new string[] {
                    s.TrustContracId.ToString(),
                    s.Trust.TrustName.ToString(),
                    s.ContractStartDate.ToShortDateString(),
                    s.ContractEndDate.ToShortDateString(),
                    s.ContractAmount.ToString(), 
                    s.RecoveredAmount.ToString(),
                    s.IsCompleted.ToString()
                }
            }).ToArray()
    };

    return Json(jsonData);
}

This is running fine, also generating JSON result. On the view page I am using script :

var gridimgpath = '/Scripts/jqgrid/themes/redmond/images';

var TrustId = <%= serializer.Serialize(ViewData["TrustId"]) %>;

// use date.js to calculate the values for this month
var gridDataUrl = '/NewTrustContract/JsonSalesCollection?id='+ TrustId;

$("#list").jqGrid({
    url: gridDataUrl,
    datatype: "json",
    mtype: 'GET',

    colNames: ['TrustContracId', 'Trust', 'Contract Start Date',
               'Contract End Date', 'Contract Amount', 'Recovered Amount',
               'Is Completed'],
    colModel: [
        { name: 'TrustContracId', index: 'TrustContracId', width: 50, align: 'left' },
        { name: 'Trust', index: 'Trust', width: 100, align: 'left' },
        { name: 'ContractStartDate', index: 'ContractStartDate', width: 100, align: 'left' },
        { name: 'ContractEndDate', index: 'ContractEndDate', width: 100, align: 'left' },
        { name: 'ContractAmount', index: 'ContractAmount', width: 100, align: 'left' },
        { name: 'RecoveredAmount', index: 'RecoveredAmount', width: 100, align: 'right' },
        { name: 'IsCompleted', index: 'IsCompleted', width: 100, align: 'right' }
    ],
    rowNum: 20,
    rowList: [10, 20, 30],
    imgpath: gridimgpath,
    height: 'auto',
    width: '900',
    pager: jQuery('#pager'),
    sortname: 'TrustContracId',
    viewrecords: true,
    sortorder: "desc",
    caption: "Contract"
});

ok, for this I used :

开发者_如何学编程
< table id="list" class="scroll" cellpadding="0" cellspacing="0" >< /table > 
< div id="pager" class="scroll" style="text-align:center;" >< /div  >

to hold the grid.I can even see empty grid view on .aspx page on view. but not appearing data. So is this mean i have to manually iterate JSON data in jquery. if yes, then where i can get JSON data in j query. and how to iterate it. i know onli $.each is use for iterate data. but how to get data in var? or another remedy for this functionality?

From where should i get jqgrid brief documentation for read?


It is some how done. For this success I have to use JsonResult instead of ActionResult. also need to add [AcceptVerbs(HttpVerbs.Get)] attribute before action definition. and while return I used

return Json(jsonData,JsonRequestBehavior.AllowGet);

Thats it, it is running fine now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜