如何将 JSON 模型绑定到对象和嵌套列表?
我正在寻找将 JSON 对象绑定到嵌套在对象中的列表。
Background
我有一个 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 绑定到对象模型。
这是我的 Action 方法:
[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");
});
但这不起作用。 Category 的属性已填充,但 List
未填充。
问题
我需要如何修改它才能使其正常工作?
补充信息
请注意,我还尝试发布类别和信息。单独的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);
}
I'm looking to bind a JSON object to a List nested in an object.
Background
I have a Category
class that contains a list of 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()
{
}
}
The user can select a number of Funds per Category, and then I want to POST a JSON string back to my Controller and have the ModelBinder bind the JSON to the object model.
This is my Action method:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Category category)
{
// Categoy.Id & Categoy.CountryID is populated, but not Funds is null
return Json(true); //
}
So far, I have this 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");
});
But this isn't working. The properties of Category are populated, but the List<ConfigurationFund>()
isn't being populated.
Question
How do I need to modify this to get it to work?
Supplementary Info
Just to note, I've also tried to post the Category & ConfiguredFunds separately, and it works, with something similar to the following:
$('#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");
});
In the following Action method, the ConfiguredFunds are populated, and the Category is also populated. However, the Category's List isn't populated. I need the Category & its List to be populated.
public ActionResult Edit(List<ConfigurationFund> configuredFunds, Category category)
{
return Json(true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经弄清楚了。我只需要更改
为:
基本上,我只是指定
funds
属于Category
与data["category.funds"]
I've figured it out. I just needed to change
to:
Basically, I just specified that the
funds
belong to aCategory
withdata["category.funds"]