试图从视图页中的模型访问数据

本文关键字:模型 访问 数据 视图 | 更新日期: 2023-09-27 18:13:49

我的模型中有这个类

namespace Foreclosure.Models
{
public class foreclosureList
{
    public string Area { get; set; }
    public int NumberOfListings { get; set; }
}
public class RETS_ListingsModel
{
    public RETS_ListingsModel(){} // empty COnstructor
    public static IEnumerable<foreclosureList> getForeclosureList() // making an IEnumerable list to contain the forclosure data
    {
        SqlConnection myConn;
        SqlCommand myCmd;
        SqlDataReader myReader;
            System.Collections.ArrayList aforclosureList = new System.Collections.ArrayList(); // create an array to hold data, later it will be converted to the ienumerable list. 
            string mySql =
             "Select [Area], count (*) as numberListings from RETS_Listings_full" +
             " Where ForeclosureYN = 'Y'" +
             " AND Area <> ''" +
             " Group By Area";
            myConn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            myCmd = myConn.CreateCommand();
            myCmd.CommandText = mySql;
            myConn.Open();

            myReader = myCmd.ExecuteReader();
            while (myReader.Read())
            {
               foreclosureList currentList = new foreclosureList(); // making an instance foreclosureList class and then adding the results from the query.
                currentList.Area = (string)myReader["Area"];
                currentList.NumberOfListings = (int)myReader["numberListings"];
                aforclosureList.Add(currentList); // adding the class object to the array
            }

            myReader.Close();
            myConn.Close();
            IEnumerable<foreclosureList> iforeclosureList = aforclosureList.Cast<foreclosureList>(); //converting the array back to the ienumerable list
            return iforeclosureList;
        }
    }

}

在View页面有

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Foreclosure.Models.foreclosureList>" %>

,然后显示列表的代码是

   <ul>    
<% foreach ( var moo in Model)  
  {  %>
<li><%: moo.Area  %></li>  
   <% } %>         
</ul>

但是我得到一个错误:foreach语句不能操作类型为"Foreclosure.Models"的变量。因为'止赎,模型'。

试图从视图页中的模型访问数据

没有GetEnumerator的公共定义

但我得到一个错误:CS1579: foreach语句不能对类型'止赎。模型的变量操作。因为'止赎,模型'。

不包含GetEnumerator的公共定义

试试这个:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Foreclosure.Models.foreclosureList>>" %