如何用ajax数据填充jqgrid,这是从asp.net MVC控制器获取的数据?

时间:2018-04-19 10:10:49

标签: jquery jqgrid mvcjqgrid

我试图用第一列填充jqgrid第一行作为静态数据,并将休息列填充为mvc控制器的计数。 对于呼叫控制器我已经wriiten ajax请求,但在尝试了一切后我无法填充数据。

这是我的ajax请求。



        function getCounts() {
            var products;
            $.ajax({
                dataType:'json',
                url: "/CapacityPlanning/getResouceCount2",
                data: JSON.stringify({ resource: resourceId, levelId: "Level-1" }),
                type: "POST",
                async: false,
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    products = response;
                },
                failure: function (response) {
                    //  alert("failure: "+response);
                },
                error: function (response) {

                    // alert("error: "+response);
                }
            });

            return products;
        }




这是我的jqgrid,我试图获得这些数据。



        $("#jqGridMonthlySummary2").jqGrid({
            //data: mydatatable1,
            datatype: "json",
            height: 'auto',
            width: 1200,
            colNames: ['Resources', 'Planned (Annual)', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June'],
            colModel: [
                { name: 'Resources', width: 400, align: 'center', editable: false, sortable: false, cellattr: getCounts() },
                
                { name: 'Planned (Annual)', width: 78, align: 'center',cellattr: getCounts() },
                 { name: 'July', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                 { name: 'Aug', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                 { name: 'Sep', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                    { name: 'Oct', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                  { name: 'Nov', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                   { name: 'Dec', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                 { name: 'Jan', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                 { name: 'Feb', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                  { name: 'Mar', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                  { name: 'Apr', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                  { name: 'May', width: 60, align: 'center', editable: false, cellattr: getCounts() },
                  { name: 'June', width: 60, align: 'center', editable: false, cellattr: getCounts() }],
            grouping: false,

            shrinkToFit: false,
            autowidth: false,
            forceFit: true,
            gridView: true,
            caption: "Resource Data",
            rowNum: 5
        });




这是我的控制器动作方法

        public ActionResult getResouceCount2(string resource, string levelId)
    {
        CapacityPlanningEntities cpEnt = new CapacityPlanningEntities();
        //string resource = "102398";
        //string levelId = "Level-1";
        int numberOfdays = 18;
        int count = cpEnt.getResouceCount(resource, levelId);
        int TotalCount = count * numberOfdays;
        //ViewBag.Count = count;
        //return TotalCount;
        return Json(TotalCount);
    }

1 个答案:

答案 0 :(得分:0)

您的控制器应该返回一个对象,请参阅Here

中的答案
  public getResouceCount2(string resource, string levelId)
    {
   CapacityPlanningEntities cpEnt = new CapacityPlanningEntities();
    //string resource = "102398";
    //string levelId = "Level-1";
    int numberOfdays = 18;
    int count = cpEnt.getResouceCount(resource, levelId);
    int TotalCount = count * numberOfdays;

        var jsonData = new
        {
            total = TotalCount ,
            page = 1,
            records = 2,
            rows = cpEnt 

        };

        return Json(jsonData);
    }