How to display a list using ViewBag

前端 未结 9 2024
孤街浪徒
孤街浪徒 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:34

    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...

    0 讨论(0)
  • 2020-12-05 03:34

    This is what i did and It worked...

    C#

    ViewBag.DisplaylList = listData; 
    

    javascript

    var dispalyList= @Html.Raw(Json.Encode(this.ViewBag.DisplaylList));
    for(var i=0;i<dispalyList.length; i++){
    
     var row = dispalyList[i];
     ..............
     ..............
    

    }

    0 讨论(0)
  • 2020-12-05 03:37
         //controller  You can use this way
    
        public ActionResult Index()
        {
          List<Fund> fundList = db.Funds.ToList();
         ViewBag.Funds = fundList;
            return View();
        }
    
    
    
    
    <--View ; You can use this way html-->
    
    @foreach (var item in (List<Fund>)ViewBag.Funds)
    {
        <p>@item.firtname</p>
    }
    
    0 讨论(0)
提交回复
热议问题