Fetch all the value from database to webgrid of ASP MVC ASPX

瘦欲@ 提交于 2019-12-08 13:57:32

问题


Hi I am getting only the last value from the database in the webgrid. I have used foreach to get all the value from the database and binded it to the webgrid but it is showing only the last value in the webgrid . Please guide me

 var listobject = list ;

  foreach (var m in ViewBag.Model)
  {
      var  list = new[] 
                {                     

                    new { Name = m.Name, Genre =m.Genre }                        

                };
      listobject = list;

  }


  WebGrid studentGrid = new WebGrid();
  studentGrid.Bind(listobject, autoSortAndPage: false, rowCount: 3);

%>

        <%= studentGrid.GetHtml(columns: 
new WebGridColumn[]
    {
        studentGrid.Column("Name", "name"),
        studentGrid.Column("Genre", "Genre"),

    })

%>


回答1:


On every iteration, you reset "list", which is why you are losing any value assigned to it on the previous iteration. Just pass in ViewBag.Model:

var grid = new WebGrid(ViewBag.Model);

@grid.GetHtml()


来源:https://stackoverflow.com/questions/14351486/fetch-all-the-value-from-database-to-webgrid-of-asp-mvc-aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!