【问题标题】:How to ModelBind JSON to an object and nested List?如何将 JSON 模型绑定到对象和嵌套列表?
【发布时间】:2011-12-02 19:57:21
【问题描述】:

我希望将 JSON 对象绑定到嵌套在对象中的 List。

背景

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

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 回我的控制器,并让 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&lt;ConfigurationFund&gt;() 未填充。

问题

我需要如何修改它才能让它工作?

补充信息

请注意,我还尝试单独发布 Category & 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);
}

【问题讨论】:

    标签: c# asp.net-mvc json model-binding


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2019-04-01
      相关资源
      最近更新 更多