0

我正在尝试用对象列表填充 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() 那么它不会显示任何内容。请有人告诉我哪里出错了。

4

2 回答 2

1

将您的学生模型更新为,这应该可以正常工作

命名空间 WebGridExample.Models { public class Student { public string Name {set;get;} public string ID{set;get;} public string Age{set;get;}

    public Student() { 

    }
    public Student(string Name,string ID,string Age) {

        this.Name = Name;
        this.Age = Age;
        this.ID = ID;
    }
}

}

谢谢,pavan pavanarya.wordpress.com

于 2012-08-09T08:37:40.037 回答
0

我得到了同样的错误。

我修改了 Student 类以删除构造函数并定义 getter/setter,它工作正常:

public class Student
{
    public string Name { get; set; }
    public string ID { get; set; }
    public string Age { get; set; }
}
于 2012-08-09T08:43:41.827 回答