Asp.net mvc 2 foreach语句无法对“”类型的变量进行操作,因为“”不包含“GetEnumerator”的公共定义

Asp.net mvc 2 foreach语句无法对“”类型的变量进行操作,因为“”不包含“GetEnumerator”的公共定义,asp.net-mvc-2,Asp.net Mvc 2,我犯了这样的错误 描述:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码 Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'EventListing.Models.EventInfo' because 'EventListing.Models.EventInfo' does not contain a public definition for

我犯了这样的错误 描述:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'EventListing.Models.EventInfo' because 'EventListing.Models.EventInfo' does not contain a public definition for 'GetEnumerator'

Source Error:

Line 106:                </th>
Line 107:            </tr>
Line 108:            <% foreach (var model in Model)
Line 109:               { %>
Line 110:            <tr>
模型

查看页面

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<EventListing.Models.EventInfo>" %>
 <% foreach (var model in Model)
               { %>
            <tr>
                <td>
                    <%= Html.ActionLink(Model.TITLE,"Detail", new {id = Model.EVENT_ID})%>

如何解决这个问题,我使用的是MVC2框架。

大概您要做的是:

 foreach(var model in Model.EventList()) {..}
虽然很难确定


如果需要使用的语法,则模型必须是具有GetEnumerator方法的类的对象。

需要将集合绑定为模型。查看此帖子了解详细信息:

看法

或者为模型使用另一种类型,并在其中定义属性列表事件。然后按以下方式迭代:

<% foreach (var model in Model.Events)
    { %>
此外,Visual Studio还可以为您执行以下操作:


你能简单解释一下吗?因为我对这项技术还不熟悉,这是个问题?没有得到它的名称,你能简单地解释一下吗,你需要绑定一个项目列表,所以你的模型应该是集合,或者你的模型应该是容器类型,它对你的EventInfo集合有属性。Visual Studio也可以为你生成这样的视图。检查屏幕截图。
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IList<EventListing.Models.EventInfo>>" %>
public ActionResult List()
{
   return this.View(EventList());
}
<% foreach (var model in Model.Events)
    { %>