Author Topic: Unable to load data into Grid from MVC JsonResult  (Read 3114 times)

Eagle_f90

  • Newbie
  • *
  • Posts: 15
    • View Profile
Unable to load data into Grid from MVC JsonResult
« on: January 22, 2016, 01:16:45 am »
After digging around on the board I was able to modify a demo to call into an MVC 6 JsonResult Action but I can not get the data to load into the grid. I can enter debug mode in Visual Studio and do see the AJAX call into the Action and that the action is returning a result containing 1 record (which is the right amount of records). The grid then load into the web page but no data inside is displayed just the words "No rows to Display." What am I doing wrong?

JavaScript Code:

Code: [Select]
<script>
    $(function ()
    {
        var colModel = [
            { title: "ID", width: 100, dataType: "integer", dataIndx: "ID" },
            { title: "Name", width: 500, dataType: "string", dataIndx: "Name" }
        ];

        var dataModel =
        {
            location: "remote",
            dataType: "json",
            method: "POST",
            contentType: "application/json; charset=UTF-8",
            url: '@Url.RouteUrl("GetChangeOrders")',
            getData: function (dataJson)
            {
                var data = dataJson.data;
                return { data: data };
            }
        };

        var obj =
        {
            title: "Test",
            dataModel: dataModel,
            colModel: colModel,
        };

        $("#Grid").pqGrid(obj);

    });
</script>

c# Code:
Code: [Select]
        [Route("GetChangeOrders", Name = "GetChangeOrders")]
        public JsonResult ChangeOrders()
        {
            var ChangeOrdersList = _DbContext.ChangeOrders.Select(co => new { ID = co.ID, Name = co.Name }).ToList();

            return Json(ChangeOrdersList);
        }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Unable to load data into Grid from MVC JsonResult
« Reply #1 on: January 22, 2016, 01:35:10 pm »
in your case:

Code: [Select]
getData: function (dataJson)
{
      return { data: dataJson };
}

Eagle_f90

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Unable to load data into Grid from MVC JsonResult
« Reply #2 on: January 22, 2016, 07:02:40 pm »
Thank you very much, that worked.