title: ASP.NET MVC Cast BaseViewModel to DerivedViewModel
body: <p>I'm developing a registration flow where user comes and fills 5 pages to complete a process. I decided to have multiple views and one controller and a ProcessNext action method to go step by step. Each time Process Next gets called it gets the origin view and next view. Since each view associated with there own view model i have created a base view model which all view specific view model derived from. Now the issue is, casting is throwing an exception.. here is the sample code</p>
  
  <p>Base View Model</p>
  
  <pre><code>public class BaseViewModel
  {
       public string viewName;
  }
  </code></pre>
  
  <p>Personal View Model </p>
  
  <pre><code>public class PersonalViewModel : BaseViewModel
  {
      public string FirstName;
      // rest properties comes here
  }
  </code></pre>
  
  <p>Index.cshtml</p>
  
  <pre><code>@Model PersonalViewModel
  
     @using (Html.BeginForm("ProcessNext", "Wizard", FormMethod.Post, new { class = "form-horizontal", role = "form" }))
  @Html.TextBoxFor(m =&gt; m.FirstName, new { @class = "form-control" })
  &lt;input type="submit" class="btn btn-default" value="Register" /&gt;
  </code></pre>
  
  <p>Basically, I'm binding the view with PersonalViewModel here</p>
  
  <p>Now in Controller ProcessNext Action method looks like this.</p>
  
  <pre><code>public ActionResult ProcessNext(BaseViewModel viewModelData)
  {
    PersonalViewModel per = (PersonalViewModel) viewModelData;
  }
  </code></pre>
  
  <p>This is failing and throwing a type case exception, why?.. </p>
  
  <p>My idea is to use only one action method to transform all these derived view model and send to a common class to validate and process. Please help me to get through this issue.. Thanks!</p>