我正在尝试用对象列表填充 webgrid。我的 HomeController 是
namespace WebGridExample.Controllers
{
public class HomeController : Controller
{
private readonly List<Student> list;
public HomeController() {
list = new List<Student>()
{
new Student(){ID="111",Name="Tanu",Age="24"},
new Student(){ID="122",Name="Danu",Age="20"},
new Student(){ID="11",Name="Taniya",Age="18"},
new Student(){ID="888",Name="Vidushi",Age="25"},
};
}
public ActionResult Index()
{
return View(list.ToList());
}
}
}
我的学生模型是:
namespace WebGridExample.Models
{
public class Student
{
public string Name;
public string ID;
public string Age;
public Student() {
}
public Student(string Name,string ID,string Age) {
this.Name = Name;
this.Age = Age;
this.ID = ID;
}
}
}
我的索引视图是
@model List<WebGridExample.Models.Student>
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<style type="text/css">
.webGrid{margin:4px;border-collapse:collapse;width:300px;}
.header{background-color:#E8E8E8;font-weight:bold;color:#FFF}
.alt{background-color: #E8E8E8;color:#000}
</style>
</head>
<body>
@{
var grid = new WebGrid(source:Model, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.NextPrevious);
}
<p>Web Grid</p>
<div id="webgrid">
@grid.GetHtml(tableStyle: ".webGrid", headerStyle: ".header", alternatingRowStyle: ".alt", columns: grid.Columns(grid.Column("Name"), grid.Column("ID"), grid.Column("Age")));
</div>
</body>
</html>
在这我在这一行得到一个错误
@grid.GetHtml(tableStyle: ".webGrid", headerStyle: ".header", alternatingRowStyle: ".alt", columns: grid.Columns(grid.Column("Name"), grid.Column("ID"), grid.Column("Age")));
说 Name 列不存在,以及如果我只给出 @grid.getHtml() 那么它不会显示任何内容。请有人告诉我哪里出错了。