我正在寻找将JSON对象绑定到嵌套在对象中的List。
背景
我有一个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字符串POST回我的Controller并让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>()
。
问题
我如何修改它以使其起作用?
补充信息
请注意,我也试图发布类别&amp; 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。但是,类别的列表未填充。我需要分类&amp;它的列表将被填充。
public ActionResult Edit(List<ConfigurationFund> configuredFunds, Category category)
{
return Json(true);
}
答案 0 :(得分: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
属于Category
data["category.funds"]