How to display a list using ViewBag

前端 未结 9 2044
孤街浪徒
孤街浪徒 2020-12-05 03:07

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 list = ne         


        
9条回答
  •  星月不相逢
    2020-12-05 03:29

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

    @((ViewBag.data as ICollection).First().FirstName)
    

    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:

      @foreach (var person in ViewBag.data) {
    • @person.FirstName
    • }

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

提交回复
热议问题