2011-12-02 40 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"]