【问题标题】:Model Binding a json array containing different object types in Asp.Net MVC模型在 Asp.Net MVC 中绑定包含不同对象类型的 json 数组
【发布时间】:2011-09-20 13:20:04
【问题描述】:

我有一个包含整数和对象的 json 数组。

[1,2,3,{Name:"russia",Value:6},{Name:"usa",Value:"8"}]

我还有以下服务器端类

class country {
  string Name;
  int Value;
}

我应该如何将 json 数组绑定到服务器端参数?我尝试在服务器上使用List<object>。它很好地绑定了整数,但没有创建国家/地区实例。而是创建原始对象并将其添加到列表中。

谢谢。

【问题讨论】:

  • 有没有办法通过创建自定义值提供者来做到这一点?
  • 这对于将数据传递给 Flot 图表尤其重要——整个数据系列是一个混合类型的数组,下面的第一个解决方案永远不会工作。

标签: asp.net json asp.net-mvc-3


【解决方案1】:

你可以试试这样的:

格式化您的控制器操作以接受 List<int> 为整数值和 List<Country> 为您的 Country 对象。

public ActionResult Index(List<int> intValues, List<Country> countryValues)

然后构建您的 JSON,使其包含整数数组和国家对象数组:

var postData = {
    intValues: [1, 2, 3],
    countryValues: [
        { Name: 'USA', Value: 6 },
        { Name: 'Russia', Value: 8 }
    ]
};

并执行一个简单的 AJAX 调用来发送数据

$(function() {
    $.ajax({
        type: 'POST',
        url: "@Url.Action("Create")",
        contentType: "application/json",
        data: JSON.stringify(postData)
    });
});

【讨论】:

  • 感谢克的回复。我知道这个解决方案。我想知道是否有一种方法可以覆盖默认模型绑定行为以获取 List。只要创建国家实例并将其向上转换为“对象”,我就可以了。
【解决方案2】:

好的,我终于解决了。我创建了一个从默认模型绑定器派生的自定义模型绑定器来完成此操作。这是代码。

public ActionResult Home(NonHomogeneousList input)

[ModelBinder(typeof(CustomModelBinder))]
class NonHomogeneousList : List<object>

public class CustomModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            NonHomogeneousList model = (NonHomogeneousList)base.BindModel(controllerContext, bindingContext);
            if (model.Members != null)
            {
                IModelBinder countryBinder = Binders.GetBinder(typeof(Country));

            // find out if the value provider has the required prefix
            bool hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
            string prefix = (hasPrefix) ? bindingContext.ModelName + "." : "";

            for (var i = 0; i < model.Count; i++)
            {
                var member = model.Members[i];
                if (member.GetType().Equals(typeof(object)))
                {
                    var subKey =  CreateSubIndexName( prefix , i);
                    ModelBindingContext innerContext = new ModelBindingContext()
                    {
                        ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(Country)),
                        ModelName = subKey,
                        ModelState = bindingContext.ModelState,
                        PropertyFilter = bindingContext.PropertyFilter,
                        ValueProvider = bindingContext.ValueProvider,
                    };
                    var country = countryBinder.BindModel(controllerContext, innerContext);
                    model.Members[i] =  country;
                }
            }
        }
        return model;
    }
}
}

【讨论】:

    猜你喜欢
    • 2017-04-29
    • 1970-01-01
    • 2013-05-27
    • 2011-01-06
    • 2011-12-09
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多