我正在尝试用 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/