Asp.net mvc 3 在MVC3中,getHtml giviing列不';不存在
我试图用对象列表填充webgrid。 我的家庭控制器是Asp.net mvc 3 在MVC3中,getHtml giviing列不';不存在,asp.net-mvc-3,Asp.net Mvc 3,我试图用对象列表填充webgrid。 我的家庭控制器是 namespace WebGridExample.Controllers { public class HomeController : Controller { private readonly List<Student> list; public HomeController() { list = new List<Student>()
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());
}
}
}
我的索引视图是
@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(),那个么名称列不存在,它不会显示任何内容。请有人告诉我哪里出了问题。将您的学生模型更新为,这样应该可以正常工作 命名空间WebGridExample.Models { 公立班学生 { 公共字符串名称{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;
}
}
}
谢谢,
帕万
我也犯了同样的错误
我修改了Student类以删除构造函数并定义getter/setter,它工作得很好:
public class Student
{
public string Name { get; set; }
public string ID { get; set; }
public string Age { get; set; }
}
谢谢Yasser。但不幸的是,当我运行应用程序时,我得到了一个空白屏幕。没有显示任何内容。如果您正在尝试任何新的内容,或者您仍然在谈论上一个代码,请更新代码?
public Student() {
}
public Student(string Name,string ID,string Age) {
this.Name = Name;
this.Age = Age;
this.ID = ID;
}
}
public class Student
{
public string Name { get; set; }
public string ID { get; set; }
public string Age { get; set; }
}