开发者

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

I Got an Error Like This Desc开发者_如何学Goription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

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>

Model

public static List<EventInfo> EventList()
        {
            var list = new List<EventInfo>();
            Dbhelper DbHelper = new Dbhelper();
            DbCommand cmd = DbHelper.GetSqlStringCommond("SELECT * FROM WS_EVENTINFO");
            DbDataReader Datareader = DbHelper.ExecuteReader(cmd);
            while (Datareader.Read())
            {
                EventInfo eventinfo = new EventInfo()
                {
                    EVENT_ID = Convert.ToInt32(Datareader[

View Page

<%@ 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})%>

How can solve this Issue, i'm Using MVC2 Framework.


You need to bind a collection as a model. Check this post for details: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IList<EventListing.Models.EventInfo>>" %>

Controller

public ActionResult List()
{
   return this.View(EventList());
}

Or use some another type for model and define property List<EventInfo> Events in it. Then iterate in following way:

<% foreach (var model in Model.Events)
    { %>

Also, Visual Studio can do it for you:

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

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


Presumably what you are trying to do is:

 foreach(var model in Model.EventList()) {..}

Although it is difficult to be sure.

If you require the syntax you used then Model will have to be an object of a class with a GetEnumerator() method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜