将对象数组从JavaScript发送到C#控制器

时间:2016-05-18 18:07:09

标签: javascript c# asp.net-mvc

我正在尝试几种方法将对象列表传递给控制器​​但是 控制器始终收到null

这是我的代码:

查看:

$('#btnSave').click(function () {

       var arrPropIdPos = new Array();         

        $('.property').each(function () {
            var obj = {
                PropertyId : $(this).attr('PropertyId'),
                Xposition : $(this).attr('xPos'),
                Yposition : $(this).attr('yPos')

            };
            arrPropIdPos.push(obj);
        });

        var jsonData = JSON.stringify(arrPropIdPos);


       $.ajax({
           url: "/PropertyPosition/Insert/",
           type: 'POST',         
           data: jsonData,
           dataType: 'json',
           //...                
        },

的console.log(arrPropIdPos);

0: Object
 PropertyId: "1"
 Xposition: "11"
 Yposition: "55"
__proto__:  Object

1: Object
 PropertyId: "2"
 Xposition: "651"
 Yposition: "48"
__proto__:  Object

的console.log(jsonData);

[{"PropertyId":"1","Xposition":"11","Yposition":"55"},
{"PropertyId":"2","Xposition":"651","Yposition":"48"}]

控制器(选项1):

[HttpPost]
public JsonResult Insert(string jsonData)
{
  // jsonData is null
}

控制器(选项2):

  public class PropertyPositionInsert
    {
        public string PropertyId { get; set; }
        public string Xposition { get; set; }
        public string Yposition { get; set; }
    }

   public JsonResult Insert(List<PropertyPositionInsert> model)
    {
      // model is null
    }

4 个答案:

答案 0 :(得分:1)

尝试将数组包装到具有属性模型的对象中:

var jsonData = JSON.stringify({model: arrPropIdPos});

还尝试将dataType选项设置为&#39; application / json&#39;。

答案 1 :(得分:1)

 $('#btnSave').click(function () {

    var arrPropIdPos = new Array();

    $('.property').each(function () {

        var obj = {
             'PropertyId' : $(this).attr('PropertyId')
            ,'Xposition' : $(this).attr('xPos')
            ,'Yposition' : $(this).attr('yPos')

        };

        arrPropIdPos.push(obj);
    });    
    $.ajax({
            type: 'POST',
            contentType: "application/json",
            data: JSON.stringify(arrPropIdPos),
            url: "/PropertyPosition/Insert/"                                    
        });

<强>类别:

  public class PropertyPositionInsert
    {
        public string PropertyId { get; set; }
        public string Xposition { get; set; }
        public string Yposition { get; set; }

    }

<强>控制器

 public JsonResult Insert(PropertyPositionInsert[] model)
    {
    }

答案 2 :(得分:0)

我过去遇到过这种情况,并且发现如果你设置了

traditional: true

在你的jQuery ajax调用中,它可以修复这个问题。

答案 3 :(得分:0)

$('#btnSave').click(function () {

   var arrPropIdPos = new Array();         

    $('.property').each(function () {
        var obj = {
            PropertyId : $(this).attr('PropertyId'),
            Xposition : $(this).attr('xPos'),
            Yposition : $(this).attr('yPos')

        };
        arrPropIdPos.push(obj);
    });

    var jsonData = JSON.stringify(arrPropIdPos);


    $.ajax({
    beforeSend: function () {  
    },
    type: "POST",
   "async": true,
    url: "/PropertyPosition/Insert/",
    data: JSON.stringify(obj),
    dataType: "json",
    contentType: "application/json",
    success: function (data) {
    },
    error: function (xhr, textStatus, error) {

    },
    complete: function () {
              },

})

<强>控制器: 创建一个类或模型

public class PropertyPositionInsert
{
    public string PropertyId { get; set; }
    public string Xposition { get; set; }
    public string Yposition { get; set; }
}
public IHttpActionResult JsonResult (PropertyPositionInsert obj)
{
 obj.PropertyId;
 obj.Xposition ;
 obj.Yposition; 
}

你可以查看上面obj的值

相关问题