10

I can have a base class for views in an MVC project like this:

public class BaseViewPage : System.Web.Mvc.ViewPage
{
  public string Something { get; set; }
}

And then in the ASPX I can do this:

<%@ Page Something="foo" Language="C#" Inherits="MyNamespace.BaseViewPage" %>

This works fine; the problem is when I try to do the same with the generic version:

public class BaseViewPage<TModel> : 
    System.Web.Mvc.ViewPage<TModel> where TModel : class
{
  public string Something { get; set; }
}

When I try to use this from the ASPX, like this:

<%@ Page Something="foo" Language="C#" 
    Inherits="MyNamespace.BaseViewPage<SomeClass>" %>

I get the error message:

Error parsing attribute 'something': Type 'System.Web.Mvc.ViewPage' does not have a public property named 'something'.

Notice how it tries to use System.Web.Mvc.ViewPage rather than my BaseViewPage class. To make it more interesting, if I remove the "Something" attribute from the Page directive and put some code in the generic version of BaseViewPage (OnInit for example) the class is actually called / used / instantiated.

So, am I doing something wrong or is this a limitation / bug? Thanks.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
pbz
  • 8,865
  • 14
  • 56
  • 70
  • possible duplicate of [Generic Inherited ViewPage<> and new Property](http://stackoverflow.com/questions/1480373/generic-inherited-viewpage-and-new-property) – James Khoury Aug 18 '12 at 02:16

1 Answers1

7

I had this exact same question a few months ago. Drove me nuts until Justin Grant answered it here.

Community
  • 1
  • 1
Omar
  • 39,496
  • 45
  • 145
  • 213
  • Very nice; thanks a lot! I tried to search SO before posting, but I guess I didn't use the right words... – pbz Feb 05 '10 at 01:36