-1

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

    })

%>

Anish
  • 768
  • 3
  • 12
  • 34

1 Answers1

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()
Mike Brind
  • 28,238
  • 6
  • 56
  • 88