2

I have this Model:

public class ChildClass : BaseClass
{
}

This Controller method:

public ActionResult ChildClassMethod(ChildClass model)
{
        return PartialView("Partials/BaseClassView", model);
}

And this View:

@model BaseClass

I get the exception that I'm passing in the wrong model... is there any way for me to make this work?

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
  • 1
    possible duplicate of [ASP.NET MVC 3: DefaultModelBinder with inheritance/polymorphism](http://stackoverflow.com/questions/5460081/asp-net-mvc-3-defaultmodelbinder-with-inheritance-polymorphism) – jamesSampica Aug 25 '14 at 17:02
  • You get the exception when you are passing the model to the view, or when you are passing the values back to the controller? – romanoza Aug 25 '14 at 17:11
  • What you can try is in "BaseClassView" view do a Html.Action that will return a partial view with a type of childclass – HaBo Aug 25 '14 at 17:39
  • Passing the `Model` to the `View` throws the exception...I only need the `BaseClass` in the `View`, though the `ChildClass` overrides certain properties of the `BaseClass`. The linkied possible duplicate is actually an unrelated issue and does not apply to this question – Serj Sagan Aug 25 '14 at 18:13

1 Answers1

1

Try an interface.

public ActionResult ChildAction(ChildClass model)
{
    return PartialView("Partials/BaseClassView", model);
}
public class ChildClass : BaseClass
{

}
public class BaseClass : IBaseClass
{

}
public interface IBaseClass
{

}

View

@model IBaseClass
Jeremy Armstrong
  • 885
  • 9
  • 22