Author Topic: Null values received on server when sent via JSON  (Read 2341 times)

dev-ncw

  • Newbie
  • *
  • Posts: 6
    • View Profile
Null values received on server when sent via JSON
« on: July 22, 2016, 05:40:54 am »
Hello,

I am trying to build a small test application. In my application I am populating the grid with static data (plz see attach). I want to post this data to server. When I try to post the data, I am receiving list of objects on the server but values of all the properties is always null (attachment 2).

Below is my code.

Model
public class GridData
    {
        public string contract { get; set; }
        public string commercialID { get; set; }
        public string commercialTitle { get; set; }
        public int spotLength { get; set; }
        public int rotationPercentage { get; set; }
        public string commercialVia { get; set; }
        public string onHand { get; set; }
        public DateTime shippingDate { get; set; }
    }

Controller Method

 public JsonResult TestGridJSONData(List<GridData> lstGridData)
        {
            Console.Write("Total objects received :: "+lstGridData.Count);
           
            return new JsonResult { Data = "Commercial Tiiitle :: " + lstGridData[0].commercialTitle };
        }

Client Side JS Code
$(function(){

    var data = [ ['98765432','12345678A15E', 'Tried and True', 30, 50, '', 'Y', ''],
            ['98765432','23456789D30E', 'Family paddling', 30, 50, '', 'N', 'Aug 3/16']];
           
    var obj = {};
    obj.width = 770;
    obj.height = 350;
    obj.showTop = true;
    obj.showBottom = true;
    obj.collapsible = true;
    obj.showHeader = true;
    obj.roundCorners = true;
    obj.rowBorders = true;
    obj.columnBorders = true;
    obj.flexHeight = true;
    obj.numberCell = { show: false };
    obj.pageModel = { type: "local" };    obj.title = "Codesheet Grid";

    obj.stripeRows = true;

    obj.colModel = [{title:"Contract", width:80, dataType:"string"},
        {title:"Commercial ID", width:120, dataType:"string"},
        {title:"Commercial Title", width:150, dataType:"string"},
        {title:"Spot Length", width:60, dataType:"integer"},
        {title:"Rotation %", width:60, dataType:"integer"},
        {title:"Commercial Via", width:135, dataType:"string"},
        {title:"On Hand? (Y / N)", width:70, dataType:"string"},
        {title:"Shipping Date", width:160, dataType:"date"}];

    obj.dataModel = {data:data};

    $("#grid_array").pqGrid( obj );
                               
    $( "#accordion" ).accordion({ heightStyle: "content" });

    $('.datepick').each(function(){ $(this).datepick(); });
    $('.daterangepick').each(function(){ $(this).datepick({ rangeSelect: true, monthsToShow: 2, showTrigger: '#calImg'}); });
   
   
});


$("#Save").click(function () {

    var s = $("#grid_array").pqGrid("option", "dataModel.data");
   
    var data = JSON.stringify(s);

   
    $.ajax({
        url: "/CodeSheet/TestGridJSONData",
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: data,

        success: function (result) {
            alert(result);
        }

    });



});

Can you please let me know what am I missing here?

Thanks,

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Null values received on server when sent via JSON
« Reply #1 on: July 22, 2016, 10:28:49 am »
Use objects with key: value pairs instead of arrays in data rows.

key names should map to the name of properties in your Model.

dev-ncw

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Null values received on server when sent via JSON
« Reply #2 on: July 22, 2016, 09:14:54 pm »
Thanks! It works.