我正在尝试使用对象列表填充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(),那么它不会显示任何内容。请有人告诉我哪里出错了。
答案 0 :(得分:1)
更新您的学生模型,这应该可以正常工作
命名空间WebGridExample.Models { 公共班学生 { public string Name {set; get;} 公共字符串ID {set; get;} 公共字符串年龄{set; get;}
public Student() {
}
public Student(string Name,string ID,string Age) {
this.Name = Name;
this.Age = Age;
this.ID = ID;
}
}
}
谢谢, 帕文 pavanarya.wordpress.com
答案 1 :(得分:0)
我得到了同样的错误。
我修改了Student类以删除构造函数并定义getter / setter,它运行良好:
public class Student
{
public string Name { get; set; }
public string ID { get; set; }
public string Age { get; set; }
}