如何将 JSON 模型绑定到对象和嵌套列表?

发布于 2024-12-19 13:18:46 字数 2696 浏览 2 评论 0原文

我正在寻找将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2024-12-26 13:18:46

我已经弄清楚了。我只需要更改

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"]

I've figured it out. I just needed to change

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");

to:

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");

Basically, I just specified that the funds belong to a Category with data["category.funds"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文