【问题标题】:trying to access data from Model in view page. .Net Mvc4试图从视图页面中的模型访问数据。 .Net Mvc4
【发布时间】:2013-05-15 14:37:55
【问题描述】:

我的模型中有这个类

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;
        }

    }


}

在我的查看页面上我有

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

但我收到一个错误: CS1579:foreach 语句无法对“Foreclosure.Models.foreclosureList”类型的变量进行操作,因为“Foreclosure.Models.foreclosureList”不包含“GetEnumerator”的公共定义

【问题讨论】:

  • 您的视图模型是 ForeclosureList,而不是 ForeclosureLists 的列表。
  • 对不起,我是新手...所以您说我正在尝试访问列表列表,而不是仅访问列表。我该如何解决?
  • 您的视图模型被声明为 ONE foreclosureList 的视图,但在您看来,您将其视为此类列表。将继承的类型更改为 System.Web.Mvc.ViewPage&lt;List&lt;Foreclosure.Models.foreclosureList&gt;&gt;

标签: c# asp.net asp.net-mvc


【解决方案1】:

但我收到一个错误:CS1579:foreach 语句无法对“Foreclosure.Models.foreclosureList”类型的变量进行操作,因为“Foreclosure.Models.foreclosureList”不包含“GetEnumerator”的公共定义

试试这个:

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

【讨论】:

  • 完美!非常感谢!
猜你喜欢
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 2016-08-11
  • 2013-04-10
  • 2014-04-19
  • 1970-01-01
相关资源
最近更新 更多