2011-12-02 38 views
0

我在找一个JSON对象绑定到嵌套在一个对象列表。如何建模绑定JSON到一个对象和嵌套列表?

背景

我有一个Category类,它包含的ConfigurationFunds列表:

public class Category 
{ 
    public int Id { get; set; } 
    public string CountryId { get; set; } 
    public string Name { get; set; } 

    public List<ConfigurationFund> Funds { get; set; } 

    public Category() 
    { 
     Funds = new List<ConfigurationFund>(); 
    } 
} 

public class ConfigurationFund 
{ 
    public int Id { get; set; } 
    public string CountryId { get; set; } 
    public string Name { get; set; } 

    public ConfigurationFund() 
    { 

    } 
} 

用户可以选择号码每个类别的基金,然后我要发布一个JSON字符串回到我的控制器,并让ModelBinder将JSON绑定到对象模型。

这是我的操作方法:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(Category category) 
{ 
    // Categoy.Id & Categoy.CountryID is populated, but not Funds is null 
    return Json(true); // 
} 

到目前为止,我有这个jQuery的:

$('#Save').click(function (e) { 
    e.preventDefault(); 

    var data = {}; 

    data["category.Id"] = $('#CategorySelector').find(":selected").val(); 
    data["category.countryId"] = $('#CategorySelector').find(":selected").attr("countryId"); 

    var funds = {}; 
    $('#ConfiguredFunds option').each(function (i) { 
     funds["funds[" + i + "].Id"] = $(this).val(); 
     funds["funds[" + i + "].countryId"] = $(this).attr("countryId"); 
    }); 

    data["category.funds"] = funds; 

    $.post($(this).attr("action"), data, function (result) { 
     // do stuff with response 
    }, "json"); 
}); 

但是,这是行不通的。类别的属性被填充,但没有被填充List<ConfigurationFund>()

问题

我如何需要修改这个来得到它的工作?

补充信息

只是要注意的,我也试着& ConfiguredFunds分别发布范畴,和它的工作原理,与类似以下的东西:

$('#Save').click(function (e) { 
    e.preventDefault(); 

    var data = {}; 

    data["category.Id"] = $('#CategorySelector').find(":selected").val(); 
    data["category.countryId"] = $('#CategorySelector').find(":selected").attr("countryId"); 

    $('#ConfiguredFunds option').each(function (i) { 
     data["configuredFunds[" + i + "].Id"] = $(this).val(); 
     data["configuredFunds[" + i + "].countryId"] = $(this).attr("countryId"); 
    }); 

    $.post($(this).attr("action"), data, function (result) { 
     // do stuff with response 
    }, "json"); 
}); 

在下面Action方法,ConfiguredFunds被填充,Category也被填充。但是,类别列表不是填充的。我需要填写其列表类别&。

public ActionResult Edit(List<ConfigurationFund> configuredFunds, Category category) 
{ 
    return Json(true); 
} 

回答

0

我已经想通了。我只是需要改变

data["category.Id"] = $('#CategorySelector').find(":selected").val(); 
data["category.countryId"] = $('#CategorySelector').find(":selected").attr("countryId"); 

var funds = {}; 
$('#ConfiguredFunds option').each(function (i) { 
    funds["funds[" + i + "].Id"] = $(this).val(); 
    funds["funds[" + i + "].countryId"] = $(this).attr("countryId"); 
}); 

data["category.funds"] = funds; 

$.post($(this).attr("action"), data, function (result) { 
    // do stuff with response 
}, "json"); 

到:

data["category.Id"] = $('#CategorySelector').find(":selected").val(); 
data["category.countryId"] = $('#CategorySelector').find(":selected").attr("countryId"); 

$('#ConfiguredFunds option').each(function (i) { 
    data["category.funds[" + i + "].Id"] = $(this).val(); 
    data["category.funds[" + i + "].countryId"] = $(this).attr("countryId"); 
}); 

$.post($(this).attr("action"), data, function (result) { 
    // do stuff with response 
}, "json"); 

基本上,我只是指定的funds属于Categorydata["category.funds"]