0

我已经很努力了,但我没有得到任何成功。

我的控制器是

public ActionResult CompOff()
        {

            return View();

        }


         [HttpPost]
        public JsonResult CompOff(RegisterCompOff r)
        {
            var compoffs = db.RegisterCompOffs.Where(l => l.Employee.Id == this.EmployeeId).Select(l => new { l.CompOffDate, l.Description, l.ExpiryDate, l.IsApproved, l.IsUtilized }).ToList();
            return Json(compoffs);

        }

我的看法是

<table id="jqgProducts" cellpadding="0" cellspacing="0"></table>
<div id="jqgpProducts" style="text-align:center;"></div>


<script src="@Url.Content("~/Scripts/jquery.jqGrid.min.js")" type="text/javascript"></script>
 <script type="text/javascript">
     $(document).ready(function () {



         $('#jqgProducts').jqGrid({
             //url from wich data should be requested
             url: '@Url.Action("CompOff")',
             //type of data
             datatype: 'json',
             //url access method type

             mtype: 'POST',
             //columns names

             colNames: ['CompOffDate', 'Description', 'ExpiryDate', 'IsApproved', 'IsUtilized'],
             //columns model
             colModel: [
                            { name: 'CompOffDate', index: 'CompOffDate', align: 'left' },
                              { name: 'Description', index: 'Description', align: 'left' },
                            { name: 'ExpiryDate', index: 'ExpiryDate', align: 'left' },
                            { name: 'IsApproved', index: 'IsApproved', align: 'left' },

                            { name: 'IsUtilized', index: 'IsUtilized', align: 'left' }

                          ],
             //pager for grid
             pager: $('#jqgpProducts'),
             //number of rows per page
             rowNum: 10,
             //initial sorting column
             sortname: 'CompOffDate',
             //initial sorting direction
             sortorder: 'asc',
             //we want to display total records count
             viewrecords: true,
             //grid height
             height: '100%'
         });
     });
    </script>

我在我的控制器的 post 方法中得到 json 结果,但数据没有绑定到我的网格..我看到的是一个空网格,当我尝试绑定一些本地数据时,它工作得很好,可以有人吗请帮助我

4

1 回答 1

2

您必须使用必要的参数以正确的格式返回 JSON 数据。

例如。

return Json(new{
                 total = 1,
                 page = 1,
                 records = collection.Count, // actual data count
                 rows = collection // actual data
              });

您没有返回totalpage(当前页面)recordsrows

试试这个..

[HttpPost]
public JsonResult CompOff(RegisterCompOff r)
{
        var compoffs = db.RegisterCompOffs.Where(l => l.Employee.Id == 
        this.EmployeeId).Select(l => new { l.CompOffDate, l.Description, l.ExpiryDate, 
        l.IsApproved, l.IsUtilized }).ToList();

        return Json(new{
            total = 100, // change into actual value
            page = 1, //first page
            records = compoffs.Count(), // no. of records you are returning now
            rows = compoffs // data
        });
}

并添加以下部分以查看

jsonReader : {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",  
                repeatitems: false,
                cell: "cell",
                id: "id",
                userdata: "userdata",    
               },    

于 2012-06-26T17:04:54.020 回答