我有一个自定义类学生如下:转换自定义类对象IEnuerable其在foreach语句使用
public class Student
{
public string Name {get;set;}
public string GuardianName {get;set;}
}
现在,我的数据在以下数据结构
IList<Student> studInfo=new List<Student>();
我来了已经把这个数据在viewbag
Viewbag.db=studInfo;
在视图页面,当我尝试使用
<table>
<thead>
<tr>
<td>Name</td>
<td>Guardian Name</td>
</tr>
</thead>
@foreach(var stud in ViewBag.db)
{
<tr>
<td>@stud.Name</td>
<td>@stud.GuardianName</td>
</tr>
}
</table>
有一个错误,说
Cannot implicitly convert type 'PuneUniversity.StudInfo.Student' to 'System.Collections.IEnumerable'
PuneUniversity是我的命名空间和StudInfo是应用程序的名称。请给我一个解决方案。 在此先感谢
这看起来* *就像你做正确的事Darin指出的问题除外。您确定您提供的代码正是您在失败的解决方案中获得的代码吗? –
为什么不试试列表 studInfo = new列表(); –
series0ne
啊...另一件事是,ViewBag.db是动态的(我认为...),因此,它可能不知道在你的foreach循环中,ViewBag.db的类型为列表,因为它被读为动态,因此,你可以尝试执行一个明确的演员? –
series0ne