asp.net - MVC 中不包含 'GetEnumerator' 的公共(public)定义

标签 asp.net asp.net-mvc razor

我正在尝试用 2 天的时间来解决这个问题。

错误是

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

我的 Controller

    public ActionResult List()
    {
        var model = ContactPersonManager.GetList();
        return View(model);
    }

在我的模型中

 public static ContactPersonList GetList()
    {
        ContactPersonList tempList = null;
        using (var myConnection = new SqlConnection(AppConfiguration.ConnectionString))
        {
            var myCommand = new SqlCommand("uspContactPersonSelectList", myConnection) { CommandType = CommandType.StoredProcedure };
            myConnection.Open();
            using (var myReader = myCommand.ExecuteReader())
            {
                if (myReader.HasRows)
                {
                    tempList = new ContactPersonList();
                    while (myReader.Read())
                    {
                        tempList.Add(FillDataRecord(myReader));
                    }
                }
                myReader.Close();
            }
        }
        return tempList;
    }

我的看法

    @foreach (var person in Model)
    {
        <ul>
            <li>
                @person.ID;
            </li>
        </ul>
    }

我错过了还是做错了?

最佳答案

而不是

@model mvc_ado.net_crud.Models.BusinessObject.ContactPerson

@model IEnumerable<mvc_ado.net_crud.Models.BusinessObject.ContactPerson>

在你看来。

这假设您的 ContactPersonList实现IEnumerable<ContactPerson> 。如果没有,那么你可以尝试

@model ContactPersonList

但是尚不清楚自定义类型是什么,ContactPersonList ,确实如此。

关于asp.net - MVC 中不包含 'GetEnumerator' 的公共(public)定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22483456/

相关文章:

c# - 从 MVC Controller 返回 FileStreamResult 时文件大小变为零

c# - 在布局 View 上填充 ViewBag

asp.net-mvc - 在 Razor 生成的 JavaScript 字符串中输出单引号

asp.net - 单击 GridView 行上的任意位置即可进入编辑模式

javascript - 数据库变化SignalR实时反馈

c# - 为总和创建随机数

c# - 如何在 C# 中获取以前的 url?

jquery - MVC : How to bind array in hidden form field value to model collection property

c# - 如何将字符串数组从 jQuery 传递到 Controller

asp.net-mvc - 带有资源文件的模型的单选按钮枚举助手