2012-08-09 14 views
0

我試圖用對象列表填充webgrid。 我的HomeController是在MVC3中,getHtml提供的列不存在

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"))); 
得到一個錯誤

說名稱列不存在以及如果我只給@ grid.getHtml()然後,它不會顯示任何東西。請有人告訴我我哪裏錯了。

+1

看到這個http://pavanarya.wordpress.com/2012/07/04/displaying-data -in-a-grid-using-webgrid-in-cshtml/ – Yasser 2012-08-09 07:32:15

+0

感謝Yasser。但不幸的是,當我運行應用程序時,我變得空白屏幕。沒有什麼顯示。 – Aada 2012-08-09 07:42:38

+0

請回復,... – Aada 2012-08-09 07:56:15

回答

1

更新你的學生模型,這應該能正常運行

命名空間WebGridExample.Models { 公共類學生 { 公共字符串名稱{設置;獲得;} 公共字符串ID {設置;獲得;} 公共字符串年齡{設置;獲得;}

public Student() { 

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

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

}

謝謝, 帕 pavanarya.wordpress.com

0

我得到了同樣的錯誤。

我修改學生類去除構造函數和定義getter/setter方法,它工作得很好:

public class Student 
{ 
    public string Name { get; set; } 
    public string ID { get; set; } 
    public string Age { get; set; } 
} 
相關問題