2016-05-18 39 views
0

をJavaScriptからオブジェクトの配列を送りますは、私は、コントローラへのオブジェクトのリストを渡すには、いくつかの方法をしようとしていたが、 コントローラは常に私のコードです。ここ<code>null</code></p> <p>を受けてC#のコントローラへ

$('#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 
    } 

答えて

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) 
    { 
    } 
1

プロパティモデルとオブジェクトにあなたの配列を包むようにしてください:

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

また、dataTypeオプションを 'application/json'に設定しようとしました。

+0

それは配列で動作します。私の配列はオブジェクトのリストです。 – Eyal

0

は、私が過去にこれに遭遇していると、あなたのjQueryのAJAX呼び出しに

traditional: true 

を設定した場合、それは問題を修正shjouldことを見出しました。

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() { 
       }, 

}) 

コントローラー: あなたはobjを上記の値を確認することができます

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; 
} 

クラスやモデルを作成します

関連する問題

 関連する問題