I have a custom class student as below:
public class Student
{
public string Name {get;set;}
public string GuardianName {get;set;}
}
Now, I have data coming in the following data structure
IList<Student> studInfo=new List<Student>();
I have put this data in the viewbag
Viewbag.db=studInfo;
On the view page, when I try to use
<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>
There is an error, saying
Cannot implicitly convert type 'PuneUniversity.StudInfo.Student' to 'System.Collections.IEnumerable'
PuneUniversity is my namespace and StudInfo is the Application name. Please suggest me a solution.
Thanks in advance
The following line is unlikely to compile:
You cannot create instances of interfaces. So I guess that your actual code is different from what you have shown here.
Also I would recommend you using strongly typed views instead of ViewBag:
and now make your view strongly typed: