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?
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?
精彩评论