title: Pass subclass of generic model to razor view
body: <p><strong>The Big Picture:</strong>  </p>
  
  <p>I have found what seems like a limitation of Razor and I am having trouble coming up with a good way around it. </p>
  
  <p><strong>The Players:</strong>  </p>
  
  <p>Let's say I have a model like this:</p>
  
  <pre><code>public abstract class BaseFooModel&lt;T&gt;
      where T : BaseBarType
  {
      public abstract string Title { get; } // ACCESSED BY VIEW
      public abstract Table&lt;T&gt; BuildTable();
  
      protected Table&lt;T&gt; _Table;
      public Table&lt;T&gt; Table // ACCESSED BY VIEW
      {
          get
          {
              if (_Table == null)
              {
                  _Table = BuildTable();
              }
              return _Table;
          }
      }
  }
  </code></pre>
  
  <p>And a subclass like this:</p>
  
  <pre><code>public class MyFooModel : BaseFooModel&lt;MyBarType&gt;
  {
      // ...
  }
  
  public class MyBarType : BaseBarType
  {
      // ...
  }
  </code></pre>
  
  <p>I want to be able to pass <code>MyFooModel</code> into a razor view that is defined like this:</p>
  
  <pre><code>// FooView.cshtml
  @model BaseFooModel&lt;BaseBarType&gt;
  </code></pre>
  
  <p>But, that doesn't work. I get a run-time error saying that <code>FooView</code> expects <code>BaseFooModel&lt;BaseBarType&gt;</code> but gets <code>MyFooModel</code>. Recall that <code>MyFooModel</code> in herits from <code>BaseFooModel&lt;MyBarType&gt;</code> and <code>MyBarType</code> inherits from <code>BaseBarType</code>. </p>
  
  <p><strong>What I have tried:</strong>  </p>
  
  <p>I tried this out in non-razor land to see if the same is true, which it is. I had to use a template param in the View to get it to work. Here is that non-razor view:</p>
  
  <pre><code>public class FooView&lt;T&gt;
      where T : BaseBarType
  {
      BaseFooModel&lt;T&gt; Model;
      public FooView(BaseFooModel&lt;T&gt; model)
      {
          Model = model;
      }
  }
  </code></pre>
  
  <p>With that structure, the following <strong>does work</strong>:</p>
  
  <pre><code>new FooView&lt;MyBarType&gt;(new MyFooModel());
  </code></pre>
  
  <p><strong>My Question:</strong>  </p>
  
  <p><strong>How can I do that with Razor?</strong> How can I pass in a type like I am doing with <code>FooView</code>?<br>
  <strong>I can't</strong>, but is there any way around this? Can I achieve the same architecture somehow?</p>
  
  <p>Let me know if I can provide more info. I'm using .NET 4 and MVC 3. </p>
  
  <hr>
  
  <p><strong>EDIT:</strong><br>
  For now, I am just adding a razor view for each subclass of <code>BaseFooModel&lt;BaseBarType&gt;</code>. I'm not psyched about that because I don't want to have to create a new view every time I add a new model. </p>
  
  <p>The other option is to just take advantage of the fact that I <em>am</em> able to get this working in regular c# classes without razor. I could just have my razor view <code>@inherits</code> the c# view and then call some render method. I dislike that option because I don't like having two ways of rendering html. </p>
  
  <p>Any other ideas? I know its hard to understand the context of the problem when I'm giving class names with <code>Foo</code> and <code>Bar</code>, but I can't provide too much info since it is a bit sensitive. My apologies about that. </p>
  
  <hr>
  
  <p><strong>What I have so far, using Benjamin's answer:</strong></p>
  
  <pre><code>public interface IFooModel&lt;out T&gt; 
      where T : BaseBarModel
  {
      string Title { get; }
      Table&lt;T&gt; Table { get; } // this causes an error:
                              // Invalid variance: The type parameter 'T' must be 
                              // invariantly valid on IFooModel&lt;T&gt;.Table. 
                              // 'T' is covariant.
  }
  
  public abstract class BaseFooModel&lt;T&gt; : IFooModel&lt;T&gt;
      where T : BaseBarModel
  {
      // ...
  }
  </code></pre>
  
  <p><strong>What ended up working:</strong></p>
  
  <pre><code>public interface IFooModel&lt;out T&gt; 
      where T : BaseBarModel
  {
      string Title { get; }
      BaseModule Table { get; } // Table&lt;T&gt; inherits from BaseModule
                                // And I only need methods from BaseModule
                                // in my view. 
  }
  
  public abstract class BaseFooModel&lt;T&gt; : IFooModel&lt;T&gt;
      where T : BaseBarModel
  {
      // ...
  }
  </code></pre>