JSON对象属性为NULL

时间:2013-08-08 11:03:57

标签: jquery asp.net asp.net-mvc json

我在ASP.Net MVC中有Kendo UI网格。我试图将JSON对象传递给控制器​​。虽然我在控制器中获取了一个对象,但属性仍为NULL(FirstName,LastName)。我们如何纠正它?

注意:该对象不为null;但是对象

中的属性为null

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" }
            ]
        });

控制器

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

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您的代码与此类似:

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

此处,x不包含foo,它包含函数引用。您需要执行该函数才能获得其返回值:

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

此处,x确实包含foo

答案 1 :(得分:1)

我解决了这个问题。为了别人的利益,我会在这里提出解决方案。

当我删除NewPerson变量时,问题得到了解决。现在我直接将值赋给myPerson。下面列出了相应的网络流量。

enter image description here

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

 data: function () 
 {

    return myPerson;
 }

<强>更新

对于getJSON方法,语法如下所示,如Better JSON data structure

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

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

});