2

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 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.