Hi i need to show a list of data using viewbag.but i am not able to do it.
Please Help me..
I tried this thing:
ICollection<Learner> list = new HobbyHomeService().FetchLearner();
ICollection<Person> personlist = new HobbyHomeService().FetchPerson(list);
ViewBag.data = personlist;
and inside view:
<td>@ViewBag.data.First().FirstName</td>
But this does not show up the value and gives error saying "Model.Person doesnot contain a defibition for First()"
This is what i did and It worked...
C#
javascript
}
To put it all together, this is what it should look like:
In the controller:
Then in the view:
In your view, you have to cast it back to the original type. Without the cast, it's just an object.
ViewBag is a C# 4 dynamic type. Entities returned from it are also dynamic unless cast. However, extension methods like .First() and all the other Linq ones do not work with dynamics.
Edit - to address the comment:
If you want to display the whole list, it's as simple as this:
Extension methods like .First() won't work, but this will.
Just put a
into the ViewBag and in the View cast it back to List
simply using Viewbag data as IEnumerable<> list