LCJ
LCJ

Reputation: 22652

JSON Object Properties are coming as NULL

I have Kendo UI grid in ASP.Net MVC. I am trying to pass JSON object to controller. Though I am getting an object in the controller, the properties are coming as NULL (FirstName, LastName). How can we correct it?

Note: The object is not null; but the properties are null in the object

JSON

   var NewPerson = new Object();
   NewPerson.FirstName = "A";
   NewPerson.LastName = "B";

   var json1 = { myPerson: NewPerson };
   return json1;

JavaScript

     $("<div/>").appendTo(e.detailCell).kendoGrid({
            dataSource: {
                type: "aspnetmvc-ajax",
                transport: {

                    dataType: "json",
                    //,type: "POST"
                    read: {
                        url: "Home/GetItemsData",
                        data: function ()
                        {
                            var NewPerson = new Object();
                            NewPerson.FirstName = "A";
                            NewPerson.LastName = "B";

                            var json1 = { myPerson: NewPerson };
                            return json1;
                        }
                    }



                },
                schema: {
                    model: {
                        fields: {
                            Program: {
                                ItemID: "number",
                            },
                            ItemDescription: { type: "string" }
                        }
                    },
                    total: "Total",
                    data: "Items"
                },
                serverPaging: true,
                serverSorting: true,
                serverFiltering: true,
                pageSize: 5
            },
            scrollable: false,
            sortable: true,
            pageable: true,
            columns: [
                        { field: "ItemID", title: "Item Id", width: "70px" },
                        { field: "ItemDescription", title: "Item Description", width: "110px" }
            ]
        });

Controller

 public JsonResult GetItemsData(Person myPerson, [DataSourceRequest] DataSourceRequest request)
    {
    }

Upvotes: 0

Views: 848

Answers (2)

LCJ
LCJ

Reputation: 22652

I resolved the issue. For the benefit of others I will put the solution here.

The issue got resolved when I removed the NewPerson variable. Now I am directly assigning values to the myPerson. And the corresponding network traffic is listed below.

enter image description here

 var myPerson = {};
 myPerson.FirstName = "A";
 myPerson.LastName = "B";

 data: function () 
 {

    return myPerson;
 }

UPDATE

For getJSON Method, the syntax will be as shown below, as mentioned in Better JSON data structure

var searchCriteria = {};
searchCriteria.Accrual = "A";
searchCriteria.Brand = "B";

$.getJSON(url, searchCriteria
, function (data) {
    if (data.length) {
        alert('Success');
    }

});

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146460

If I understood correctly, your code is similar to this:

var x = function (){
  return "foo";
};

Here, x does not contain foo, it contains the function reference. You need to execute the function in order to get its return value:

var x = (function (){
  return "foo";
})();

Here, x does contain foo.

Upvotes: 1

Related Questions