tags:

views:

51

answers:

1

I have an Edit page that has the base class as the data class, and it would show different editor form depending on which derived class the model is. However, after posting

[HttpPost]
public ActionResult Edit(BaseClassModel model)

the model here only holds values for the base class and cannot be cast back to the derived class.

How can this be solved?

thank you

+1  A: 

Depending on the rest of your implementation, you can either

  • Prefer Encapsulation over Inheritance, having each of your current subclasses contain a complex property with all the common fields
  • Write your own ModelBinder (take a look at the DefaultModelBinder source for an example) and create it in Global.asax, eg: ModelBinders.DefaultBinder = new ComplexModelBinder();
  • Create a BaseClassModelBinderAttribute and mark each of your arguments with that, eg: public ActionResult Edit([BaseClassModelBinder] BaseClassModel model)
  • A combination of the above
pdr