开发者

New Error in Strongly-Typed View - Cannot Enumerate Model? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. 开发者_运维技巧 Closed 9 years ago.

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜