【问题标题】:ASP.NET Model binder cannot bind collection of complex of object containing collectionsASP.NET 模型绑定器无法绑定包含集合的复杂对象的集合
【发布时间】:2013-12-11 13:13:09
【问题描述】:

我通过对 mvc 控制器的 ajax 调用来总结 json。 JSON 包含一个属性,即 just 和 int。另一个属性是objects 的集合,objects 本身具有otherobjects 的集合,otherojbect 的属性是promitive 类型。问题是,如果我仅汇总该 json 的第二个属性(复杂 objects 的集合)模型绑定器会做得非常好并将其绑定到模型但是当我提交 objects 的集合时原始 int 模型绑定器仅绑定 int 属性,而 seccond 属性只是空集合。我使用 ext-js ajax 调用。我尝试使用params 属性和jsonData 属性提交json,似乎没有任何效果?这个问题的最佳解决方案是什么。任何帮助都会得到帮助。

public class ModelToBind
{
    public int IntParam { get; set; }
    public IEnumerable<CompexObject> ComplexObjectParam { get; set; }
}

public class CompexObject
{
    public int AnotherIntProp { get; set; }
    IEnumerable<AnotherComplexObject> AnotherComplexObject{ get; set; }
}

public class AnotherComplexObject
{
    // some primitive properties here
}

public JsonData DoSomething(ModelToBind model)
{
    model.IntParam; // this property is binded
    model.ComplexObjectParam; // this is binded as empty collection
}

【问题讨论】:

  • 请发布您的代码。
  • 这里是核心@Maess
  • 发布您的 javascript 并查看
  • 它是 javasvript 问题的可能性很大吗?因为我认为所有内容都发布得很安静,这是一个模型粘合剂问题。我无法发布 javascript。为什么需要 view ?我@Maess
  • 这可能是您发布数据的方式有问题,因此希望查看 js 或查看代码。

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


【解决方案1】:

你的动作参数是什么?

从逻辑上讲,如果你想绑定 {int - Object},你应该有一个具有类似属性的 c# 类型

public class MyType{
     public int MyPrimitive{get;set;}
     public MyOtherType ComplexType{get;set;}
}

并将此类型作为您操作的参数。 当然,其他情况也是可能的,但我们需要有关您的操作的更多信息。

【讨论】:

  • 情况并非如此。请看编辑,我已经发布了一些代码
  • 你有一个集合,它不仅仅是一个类型,我们需要看到客户端。从逻辑上讲,可以像 ComplexObjectParam [0].property=... COmplexObjectParam[1].property= 那样发布集合
猜你喜欢
  • 2014-03-07
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 2015-03-28
相关资源
最近更新 更多