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:
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...
Looks like removing code-behind file and decalring inheriting class on .aspx/.ascx page itself doesn't bring any value. Because intellisense doesn't work in this scenario.
Looks like removing code-behind file and decalring inheriting class on .aspx/.ascx page itself doesn't bring any value. Because intellisense doesn't work in this scenario.
I'm not sure what is meant by that statement. Intellisense works just fine as long as the Inherits attribute is set correctly. The easiest way I've found to do this is to use a typeof() to get a Type corresponding to the ViewPage<> you want, then to look
at its Name property. Copy + paste this string into your .aspx file. For example:
Type viewPageType =
typeof(ViewPage<IList<Class1>>);
string mangledName = viewPageType.FullName;
In my application, this produces a mangled name of System.Web.Mvc.ViewPage`1[[System.Collections.Generic.IList`1[[MvcApplication5.Models.Class1, MvcApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]. So the @ Page directive would look like:
This horrendous-looking name is how the CLR identifies types. It is independent of any language used (VB, C#, C++, dynamic languages, etc.). Since the directives are not language-specific (note that the Language attribute is just a string attribute, but
it doesn't change how the directives are parsed), a language-neutral type representation had to be chosen. This is the CLR's language-neutral representation of generics.
You just have explicitly added Version, Culture and PublicKeyToken to my example though all they are optional as far as I know. Anyway I have also added them and have same issues with Intellisense:
genericsintellisensecode-behindinheritsclr
Web and cloud application engineer, architect, hacker, entrepreneur | LinkedIn | @koistya
You just have explicitly added Version, Culture and PublicKeyToken to my example though all they are optional as far as I know.
No, I did not. Your actual type name was incorrect. Use the typeof(ViewData<>).FullName trick I mentioned earlier in this thread and you will produce the actual
correct mangled name for the type.
ghotiman
Member
205 Points
57 Posts
Typed ViewPage Using Generics Without Code Behind
Dec 13, 2007 06:08 AM|LINK
I've successfully created .aspx view pages without code behind by replacing the first line with this:
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?
MVC generics viewpage typedview viewdata generic
JeremyS
Member
506 Points
99 Posts
Re: Typed ViewPage Using Generics Without Code Behind
Dec 13, 2007 09:02 AM|LINK
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:
Hope this is useful.
ghotiman
Member
205 Points
57 Posts
Re: Typed ViewPage Using Generics Without Code Behind
Dec 13, 2007 04:55 PM|LINK
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
generics generic directive pagedirective page
JeremyS
Member
506 Points
99 Posts
Re: Typed ViewPage Using Generics Without Code Behind
Dec 13, 2007 05:08 PM|LINK
koistya
Member
153 Points
59 Posts
Re: Typed ViewPage Using Generics Without Code Behind
Oct 21, 2008 06:45 PM|LINK
Looks like removing code-behind file and decalring inheriting class on .aspx/.ascx page itself doesn't bring any value. Because intellisense doesn't work in this scenario.
Let's vote for this bug:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104071
Maybe VS dev team will fix it.
generics intellisense code-behind inherits
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Typed ViewPage Using Generics Without Code Behind
Oct 22, 2008 12:20 AM|LINK
I'm not sure what is meant by that statement. Intellisense works just fine as long as the Inherits attribute is set correctly. The easiest way I've found to do this is to use a typeof() to get a Type corresponding to the ViewPage<> you want, then to look at its Name property. Copy + paste this string into your .aspx file. For example:
Type viewPageType = typeof(ViewPage<IList<Class1>>);string mangledName = viewPageType.FullName;
In my application, this produces a mangled name of System.Web.Mvc.ViewPage`1[[System.Collections.Generic.IList`1[[MvcApplication5.Models.Class1, MvcApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]. So the @ Page directive would look like:
<%
@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage`1[[System.Collections.Generic.IList`1[[MvcApplication5.Models.Class1, MvcApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" %>This horrendous-looking name is how the CLR identifies types. It is independent of any language used (VB, C#, C++, dynamic languages, etc.). Since the directives are not language-specific (note that the Language attribute is just a string attribute, but it doesn't change how the directives are parsed), a language-neutral type representation had to be chosen. This is the CLR's language-neutral representation of generics.
koistya
Member
153 Points
59 Posts
Re: Typed ViewPage Using Generics Without Code Behind
Oct 22, 2008 10:07 AM|LINK
Here is an example:
ViewsWithoutCodeBehind.zip
Intellisense doesn't work without code-behind files and inheriting generic class declared on .aspx page in CLR way.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Typed ViewPage Using Generics Without Code Behind
Oct 22, 2008 06:35 PM|LINK
Intellisense works just fine; it's just that your @ Page directive is incorrect. It should read (note the Inherits attribute):
<%
@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage`1[[ViewsWithoutCodeBehind.Models.HomeModel, WebSite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" %>koistya
Member
153 Points
59 Posts
Re: Typed ViewPage Using Generics Without Code Behind
Oct 22, 2008 07:00 PM|LINK
You just have explicitly added Version, Culture and PublicKeyToken to my example though all they are optional as far as I know. Anyway I have also added them and have same issues with Intellisense:

generics intellisense code-behind inherits clr
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Typed ViewPage Using Generics Without Code Behind
Oct 22, 2008 07:02 PM|LINK
No, I did not. Your actual type name was incorrect. Use the typeof(ViewData<>).FullName trick I mentioned earlier in this thread and you will produce the actual correct mangled name for the type.