c# - MVC model binding - abstract collection properties -


not duplicate, see appended clarification

i bind model setup following

public class shop{     public string name {get;set;}     public icollection<product> products {get;set;} //product 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 controller so

public actionresult(){     shop model = shopfactory.getshop();     return view(model); }  [httppost] public actionresult(shop model){     //.... } 

i'm using begincollectionitem bind collection, problem arrises when posting form cannot create abstract class - namely objects inside shop.products

i've looked @ subclassing defaultmodelbinder override createmodel createmodel never called argument modeltype = product, modeltype = shop

how create modelbinder bind object has abstract collection property?

clarification
question not duplicate because not dealing abstract model, dealing model has collection of abstract objects, undergoes separate process in model binding system.

just clarify, others me stumbled on page looking solution:

mvc 3 model binding sub type (abstract class or interface) exactly looking for. not need special because of begincollectionitem.

simply write model binder whatever abstract class have collection of, , decorate abstract class with:

[modelbinder(typeof(mymodelbinder))] public abstract class myclass { ... 

then in modelbinder, use this:

string typename = bindingcontext.valueprovider.getvalue(bindingcontext.modelname + ".type").attemptedvalue; type instantiationtype = type.gettype(typename); 

to type editortemplate's hidden field:

    @html.hiddenfor(m => type) 

finally, if still can't properties of concrete types, double check in fact properties, , not fields!


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -