How to display a list using ViewBag

185,827

Solution 1

In your view, you have to cast it back to the original type. Without the cast, it's just an object.

<td>@((ViewBag.data as ICollection<Person>).First().FirstName)</td>

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:

<ul>
    @foreach (var person in ViewBag.data)
    {
        <li>@person.FirstName</li>
    }
</ul>

Extension methods like .First() won't work, but this will.

Solution 2

To put it all together, this is what it should look like:

In the controller:

List<Fund> fundList = db.Funds.ToList();
ViewBag.Funds = fundList;

Then in the view:

@foreach (var item in ViewBag.Funds)
{
    <span> @item.FundName </span>
}

Solution 3

simply using Viewbag data as IEnumerable<> list

@{
 var getlist= ViewBag.Listdata as IEnumerable<myproject.models.listmodel>;

  foreach (var item in getlist){   //using foreach
<span>item .name</span>
}

}

//---------or just write name inside the getlist
<span>getlist[0].name</span>

Solution 4

i had the same problem and i search and search .. but got no result.
so i put my brain in over drive. and i came up with the below solution. try this in the View Page

at the head of the page add this code

 @{
      var Lst = ViewBag.data as IEnumerable<MyProject.Models.Person>;
    }

to display the particular attribute use the below code

@Lst.FirstOrDefault().FirstName

in your case use below code.

<td>@Lst.FirstOrDefault().FirstName </td>  

Hope this helps...

Solution 5

Use as variable to cast the Viewbag data to your desired class in view.

@{

IEnumerable<WebApplication1.Models.Person> personlist = ViewBag.data as
IEnumerable<WebApplication1.Models.Person>;
// You may need to write WebApplication.Models.Person where WebApplication.Models is
  the namespace name where the Person class is defined. It is required so that view 
  can know about the class Person. 
}

In view write this

<td>
    @(personlist.FirstOrDefault().Name)
</td>
Share:
185,827
user1274646
Author by

user1274646

Updated on May 02, 2020

Comments

  • user1274646
    user1274646 almost 4 years

    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()"

  • user1274646
    user1274646 almost 12 years
    i didn't get u. u mean to say i should display only ViewBag.data or u mean something else..??
  • 360Airwalk
    360Airwalk almost 12 years
    oh that's mean. posting from my mobile the post was sent too early and the brackets stripped away. see edit for correction. sorry. you might alos need to use System.Linq in your View
  • user1274646
    user1274646 almost 12 years
    hey thanx i got the value but can u tell me how can i fetch all the values assigned in the list?
  • Ashok kumar
    Ashok kumar over 3 years
    Great Chris. I liked your approach, but that is not what I am looking for. Even then, one up-vote for you for your nice approach.
  • HO LI Pin
    HO LI Pin over 2 years
    Simple and neat , I like this