views:

400

answers:

2

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.

A: 

Have you got a code behind file as well? You'll need to change the base class on that too

Nope, I'm using the default views created when you make a new MVC solution; those do not have code behind.
pbz
MVC doesn't user code-behinds on the .aspx pages.
Alastair Pitts
@Alastair - that's incorrect. By convention, most people don't use code-behind - however, it's perfectly valid to create a class file and put code there in ASP.Net MVC.
womp
Huh, well there you go. Learn something new every day. Damn unable to remove downvote.
Alastair Pitts
+3  A: 

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

Baddie
Very nice; thanks a lot! I tried to search SO before posting, but I guess I didn't use the right words...
pbz