Question

Well,

Let's suppose that I have two Model classes:

public class BaseClass{
    public Int32 variable {get;set;}
}

public class DerivatedClass : BaseClass{
    public Int32 otherVariable {get;set;}
} 

And a View with BaseClass type as Model.

If I pass a DerivatedClass to the View and retrieve information through a form, it wouldn't be "castable" to DerivatedClass again?

The problem is that if I retrieve the Model's type inside the view (Model.GetType().FullName), I get (without surprises) a DerivatedClass type.

But when I check the model posted, inside my controller, I get a BaseClass (and obviously, it can't be casted!)

Controller:

public ActionResult ViewPage(){
    return View(new DerivatedClass());
}

[HttpPost]
public ActionResult ViewPage(BaseClass b){
    b.GetType().FullName;                 //Gives me Project.packeges.BaseClass.
    DerivatedClass d = (DerivatedClass)b; //Ops, It can't be done. Exception.
}

View:

@model Project.packeges.BaseClass

<h3>@Model.GetType().FullName</h3>  
<!-- Gives me Project.packeges.DerivatedClass -->
...

Is my logic wrong? There's anyway to do this cast inside the controller after retrieve the POST information?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top