2013-06-20 26 views
0

代碼的工作原理除了接收地址的子集合嗎?在json中發送這個集合的正確方法是什麼?不幸的是這種嘗試失敗在 '地址' 值爲null 感謝,如何發佈ajax調用和C#來識別子對象

var val = { 
    'forename': 'test', 
    'surname': 'test', 
    'postcode': 'test', 
    'Addresses': { 
     'Line1': 'Test', 
     'Line2': 'Here' 
    } 
}; 

jr.ajax.loadJson(url, val, 
    true, 
    function(xhr, textStatus, errorThrown) { 
    }, 
    true, 'post', val); 
}); 


// I want the posted value to be this object 

public class Member 
{ 
    public string forename { get; set; }   
    public string surname { get; set; }   
    public string postcode { get; set; } 
    public Address[] Addresses { get; set; } 
} 

public class Address 
{ 
    public string Line1 { get; set; }   
    public string Line2 { get; set; }   
} 

我的控制器看起來是這樣的:

public ActionResult Show(Member request) {..} 

回答

0

你JSON是不正確你的地址子成員。

在您的模型中,地址爲地址數組。但是,在您的JSON中,您將地址作爲單個地址發送。這是行不通的。

相反,改變你的JSON是:

var val = { 
    'forename': 'test', 
    'surname': 'test', 
    'postcode': 'test', 
    'Addresses': [ 
     { 
      'Line1': 'Test', 
      'Line2': 'Here' 
     } 
    ] 
}; 

注意周圍的地址結構的[]。這現在應該工作。

另一個考慮因素是在模型中使用List<Address>而不是Address[]