Ah, sorry. What I wrote works. However, if you replace the first line of MyList.cshtml with:
@model MyList<T>
then things aren't so smooth. I suspect this is because the code is trying to compile without yet being able to resolve T, but hope this is not the case...
The point is that it makes no sense using generics....if you use generics a lot of operations will not be allowed. You will not be able to use helpers that use lambda
expressions like TextBoxFor.
The right way to proceed when you need to do a Vie to be used with several types is by using dynamic variable, so you should do something like:
@model MyList<dynamic>
if you do this you will be allowed to do ANY operations on your List, since the actual syntactic check will be done at runtime...however also this solutions has drawbacks:
1) you are allowed to invoke any method on your helpers or type containing dynamic, but no Intellisense is available since VisualStudio don't know the actual types.
2) Extension types can't be used, this means you cannot call Html.TextBox(......but you have to call the actual static method that define the Extension Method...like
yes. only anonymous types can not be used as the model.
your sample code makes no sense. your generic has a single property, and is not a collection, so you would leave out the foreach. you would also need to define a template helper for NamedUri. if you created a NamedUri template helper and switched to List<NameUri>
as the model type, then your code would work.
Another approach is to create a list view which is strong type to interface, this interface contains the properties you want to show in the list, so any classes which implement this interface can be rendered using this view.
But if you want to allow anything to be rendered as a list, you can refer to @francesco's suggestion.
chrispike
Member
2 Points
2 Posts
Generic models in Razor views
Jun 06, 2011 04:27 PM|LINK
Is it possible to use generic models in Razor views? Trying to achieve something like this:
List.cshtml:
@model MyList<NamedUri> <ul> @foreach(var uri in Model) { <li> @Html.DisplayFor(m => m.Content); </li> } </ul>Given a MyList.cs:
public class MyList<T> { public MyList(T content) { this.Content = content } public T Content { get; set; } }and some .cshtml to render NamedUri objects, allowing anything to be rendered as a list.
RAZOR generic
egor598
Contributor
3025 Points
448 Posts
Re: Generic models in Razor views
Jun 06, 2011 05:11 PM|LINK
Yes, it should be possible to use generic models in Razor views as long as your property types can be successfully resolved/mapped to html elements.
See this link: http://msdn.microsoft.com/en-us/library/ee407390.aspx
chrispike
Member
2 Points
2 Posts
Re: Generic models in Razor views
Jun 06, 2011 06:45 PM|LINK
Ah, sorry. What I wrote works. However, if you replace the first line of MyList.cshtml with:
then things aren't so smooth. I suspect this is because the code is trying to compile without yet being able to resolve T, but hope this is not the case...
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Generic models in Razor views
Jun 06, 2011 10:31 PM|LINK
The point is that it makes no sense using generics....if you use generics a lot of operations will not be allowed. You will not be able to use helpers that use lambda
expressions like TextBoxFor.
The right way to proceed when you need to do a Vie to be used with several types is by using dynamic variable, so you should do something like:
@model MyList<dynamic>
if you do this you will be allowed to do ANY operations on your List, since the actual syntactic check will be done at runtime...however also this solutions has drawbacks:
1) you are allowed to invoke any method on your helpers or type containing dynamic, but no Intellisense is available since VisualStudio don't know the actual types.
2) Extension types can't be used, this means you cannot call Html.TextBox(......but you have to call the actual static method that define the Extension Method...like
this: InputExtensions.TextBox(Html,....
Mvc Controls Toolkit | Data Moving Plug-in Videos
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: Generic models in Razor views
Jun 07, 2011 12:01 AM|LINK
yes. only anonymous types can not be used as the model.
your sample code makes no sense. your generic has a single property, and is not a collection, so you would leave out the foreach. you would also need to define a template helper for NamedUri. if you created a NamedUri template helper and switched to List<NameUri> as the model type, then your code would work.
RAZOR generic
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: Generic models in Razor views
Jun 07, 2011 12:08 AM|LINK
the razor view does not compile to a generic class. how would you specify the type? this is what Html helpers are for.
Forest Cheng...
Star
8370 Points
819 Posts
Re: Generic models in Razor views
Jun 08, 2011 02:49 AM|LINK
Hi Chrispike,
Another approach is to create a list view which is strong type to interface, this interface contains the properties you want to show in the list, so any classes which implement this interface can be rendered using this view.
But if you want to allow anything to be rendered as a list, you can refer to @francesco's suggestion.
Hope this helpful,
Forest Cheng
If you have any feedback about my replies,please contact msdnmg@microsoft.com.
Microsoft One Code Framework