title: How to cast a ViewModel to another ViewModel?
body: <p>I have two ViewModels which are similar, and i need to cast one to another. </p>
  
  <p>This is the first one: </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace TestInheritance.Models
  {
      public class ShowMatrixQuestionViewModel : ShowQuestionViewModel
      {
          public Dictionary&lt;MatrixRows, List&lt;MatrixColumns&gt;&gt; columnrow;
          public List&lt;MatrixColumns&gt; columns;
          public List&lt;MatrixRows&gt; rows;
  
          public ShowMatrixQuestionViewModel()
          {
              columns = new List&lt;MatrixColumns&gt;();
              rows = new List&lt;MatrixRows&gt;();
              columnrow = new Dictionary&lt;MatrixRows, List&lt;MatrixColumns&gt;&gt;();
          }
      }
  
      public class MatrixColumns
      {
          public int Column_ID { get; set; }
          public int Column_Number { get; set; }
          public String Column_Description { get; set; }
          public Boolean IsAnswer { get; set; }
          public int Procent { get; set; }
      }
  
      public class MatrixRows
      {
          public bool Delete { get; set; }
          public int Row_Id { get; set; }
          public String Row_Number { get; set; }
          public String Row_Description { get; set; }
      }
  
  }
  </code></pre>
  
  <p>The second looks like this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace TestInheritance.Models.ViewModels.New
  {
      public class ShowMatrixProcentViewModel : ShowMatrixQuestionViewModel
      {
          // Dette er for at vi kan bruge DisplayTemplates
          public ShowMatrixProcentViewModel()
              : base()
          {
          }
      }
  }
  </code></pre>
  
  <p>When i try to cast a ShowMatrixQuestionViewModel to a ShowMatrixProcentViewModel i get a InvalidCastException:</p>
  
  <pre><code>MatrixColumns mc = new MatrixColumns()
              {
                  Column_Description = "Hej",
                  Column_ID = 1,
                  Column_Number = 1,
                  IsAnswer = false
              };
  
              MatrixColumns mc2 = new MatrixColumns()
              {
                  Column_Description = "Med",
                  Column_ID = 1,
                  Column_Number = 1,
                  IsAnswer = false
              };
  
              MatrixRows mr = new MatrixRows()
              {
                  Row_Description = "2005",
                  Row_Id = 1,
                  Row_Number = "1"
              };
  
              MatrixRows mr2 = new MatrixRows()
              {
                  Row_Description = "2008",
                  Row_Id = 1,
                  Row_Number = "2"
              };
  
              List&lt;MatrixColumns&gt; matrixcolumns = new List&lt;MatrixColumns&gt;();
              matrixcolumns.Add(mc);
              matrixcolumns.Add(mc2);
              List&lt;MatrixRows&gt; matrixrows = new List&lt;MatrixRows&gt;();
              matrixrows.Add(mr);
              matrixrows.Add(mr2);
  
              ShowMatrixQuestionViewModel avm = new ShowMatrixQuestionViewModel()
              {
                  IsAnswered = true,
                  Question_ID = 1,
                  Question_Number = "1",
                  Question_Type = "hej",
                  Question_Wording = "spørgsmål1",
                  Visible = true,
                  columns = matrixcolumns,
                  rows = matrixrows
              };
  
              ShowMatrixProcentViewModel sm = (ShowMatrixProcentViewModel)avm;
  
  return View(avm);
  </code></pre>
  
  <p>The exception says:</p>
  
  <p>System.InvalidCastException: Unable to cast object of type 'TestInheritance.Models.ShowMatrixQuestionViewModel' to type 'TestInheritance.Models.ViewModels.New.ShowMatrixProcentViewModel'.</p>
  
  <p>Do i need to initialize some stuff in the ShowMatrixProcentViewModel before i can cast??</p>
  
  <p>Thanks in advance</p>