问题
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