Typed ViewPage Using Generics Without Code Behind

Last post 12-13-2007 1:08 PM by JeremyS. 3 replies.

Sort Posts:

  • Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 2:08 AM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 9:36 AM
    • Missoula, MT
    • Posts 55

    I've successfully created .aspx view pages without code behind by replacing the first line with this:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

    However, trying to change it to inherit the generics to get typed view data like the following doesn't seem to work:

     <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication.Models.MyModel>" %>

    Is there some trick I'm missing here? Can you not use a generic class in the Inherits attribute?

  • Re: Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 5:02 AM
    Answer
    • Loading...
    • JeremyS
    • Joined on 10-21-2006, 4:23 AM
    • UK
    • Posts 75

    You need to use the standard CLR notation for generics. For example, I have an Error Handling View which deals with processing exceptions. The view inherits from ViewPage<Exception> so this is what you'd have in the view: 

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage`1[[System.Exception, mscorlib]]" %>

     
     Hope this is useful.

  • Re: Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 12:55 PM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 9:36 AM
    • Missoula, MT
    • Posts 55

    Thanks!  Where did you find this?  It doesn't say anything about it in the online help for the page directive.  I thought that the `1 was a typo, but it's not...

     After a little more searching I found this: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104071&wa=wsignin1.0

  • Re: Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 1:08 PM
    • Loading...
    • JeremyS
    • Joined on 10-21-2006, 4:23 AM
    • UK
    • Posts 75
    I'm glad it works for you :) I didn't actually find any information about this anywhere - I just ran into it when doing some debugging.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter