views:

94

answers:

1

I am currently using ASP.NET MVC 2.0 RC2, having recently moved from version 1.0.

I have noticed that some of my views no longer work. I have views that inherit from a strongly-typed IEnumerable, like this:

Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyProject.Models.MyType>>"

In the past, I have enumerated the model in my view like this:

<% foreach (var item in Model) { %>

But that no longer works. I get an error in the browser:

foreach statement cannot operate on variables of type 'object' because 'object' 
does not contain a public definition for 'GetEnumerator'

I can fix it by performing the requisite casts:

<% foreach (MyType item in (IEnumerable<MyType>)Model) { %>

But this is pretty ugly, and it suggests that there has been a change in behavior, so I need to know why.

I checked, and ViewPage.Model is indeed defined as object, so I'm wondering how this could have ever worked the way it did before. It's also defined as object in ASP.NET MVC 1.0.

What gives?

Note: I have the MVC source code loaded into my project. Would that make a difference? Is there some metadata that gets injected at runtime that makes this work, which is prevented from doing so by seeing the object definition in the source?

+1  A: 

FWIW, ViewPage.Model is of type TModel in ViewPage<TModel>, the generic class that you're using. (In your sample code, TModel is IEnumerable<MyProject.Models.MyType>). So it shouldn't be straight object.

Does it make a difference if you change your @Page to

Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyType>>"

and add a

<%@ Import namespace="MyProject.Models" %>

line to the view?

kevingessner
Adding the line had no effect.
Robert Harvey
`ViewPage.Model` is intellisensing as `object`. So it would seem that the page is not properly picking up the `Inherits` attribute in the `Page` directive.
Robert Harvey
In fact, none of the pages are working anymore. They are all showing ViewPage.Model as object.
Robert Harvey
I unloaded the ASP.NET MVC source projects, restored the reference to the ASP.NET 2.0 assembly in the GAC, and the problem went away.
Robert Harvey
Cool. Maybe you had an old version of the MVC source, which didn't have the generic ViewPage?
kevingessner
No, it's the latest and greatest RC2 for v2. Either there is a problem with the source (which seems unlikely), or I unwittingly did something in the source that hosed the generic ViewPage class. Oh, well.
Robert Harvey