【发布时间】:2014-11-28 19:52:36
【问题描述】:
不是重复的,请参阅附加说明
我想绑定如下模型设置
public class Shop{
public string Name {get;set;}
public ICollection<Product> Products {get;set;} //Product is abstract
}
public abstract class Product{
public string Name {get;set;}
}
public class ProductA : Product{
public string foo {get;set;}
}
public class ProductB :Product{
public string bar {get;set;}
}
还有这样的控制器
public ActionResult(){
Shop model = ShopFactory.GetShop();
return View(model);
}
[HttpPost]
public ActionResult(Shop model){
//....
}
我正在使用BeginCollectionItem 绑定集合,但是在发布表单时出现问题,因为它无法创建抽象类 - 即 Shop.Products 中的对象
我查看了子类化 DefaultModelBinder 以覆盖 CreateModel 但是 CreateModel 从未使用参数 modeltype = Product 调用,只有 modeltype = Shop
如何创建一个 ModelBinder 来绑定具有抽象集合作为属性的对象?
澄清
这个问题不是重复的,因为我们不是在处理抽象模型,而是在处理具有抽象对象集合的模型,这在模型绑定系统中经历了一个单独的过程。
【问题讨论】:
-
不是重复的。这个问题有一个抽象对象的集合。该问题的解决方案不起作用。
标签: c# asp.net asp.net-mvc