Not a duplicate, see appended clarification
I would like to bind a model setup like the following
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;}
}
And a controller like so
public ActionResult(){
Shop model = ShopFactory.GetShop();
return View(model);
}
[HttpPost]
public ActionResult(Shop model){
//....
}
I'm using BeginCollectionItem to bind the collection, however a problem arrises when POSTing the form as it cannot create an abstract class - namely objects inside Shop.Products
I've looked at subclassing DefaultModelBinder
to override CreateModel
however CreateModel is never called with the argument modeltype = Product
, only modeltype = Shop
How do I create a ModelBinder that will bind an object that has an abstract collection as a property?
Clarification
This question is not a duplicate because we are not dealing with an abstract model, we are dealing with a Model that has a collection of abstract objects, this undergoes a separate process in the model binding system.