0

オブジェクトのリストを webgrid に取り込もうとしています。私のホームコントローラーは

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

学生モデルをに更新すると、これは正常に機能するはずです

namespace 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;
    }
}

}

ありがとう、パヴァン pavanarya.wordpress.com

于 2012-08-09T08:37:40.037 に答える