2013-04-08 57 views
2

通過Kendo UI獲得了一個MVC4實體框架數據庫第一個項目。在Kendo UI網格中顯示外部數據

我說我在顯示網格模型看起來像下面:

public partial class RuleEntry 
{ 
    public RuleEntry() 
    { 
     this.RuleEntriesCases = new HashSet<RuleEntriesCas>(); 
    } 
    [Key] 
    public int ID { get; set; } 
    public string Country { get; set; } 
    public Nullable<int> Family { get; set; } 
    public Nullable<int> IP { get; set; } 
    public string RuleKey { get; set; } 
    public Nullable<int> Status { get; set; } 
    public string Title { get; set; } 

    public virtual Country Country1 { get; set; } 
    public virtual Family Family1 { get; set; } 
    public virtual IP IP1 { get; set; } 
    public virtual RuleStatus RuleStatus { get; set; } 
    public virtual ICollection<RuleEntriesCas> RuleEntriesCases { get; set; } 
} 

屬性Country是一個「SE」,是一個外鍵的表「國家」,這將包含名稱, 「瑞典」。該國模型是這樣的:

public partial class Country 
{ 
    public Country() 
    { 
     this.RuleEntries = new HashSet<RuleEntry>(); 
    } 
    [Key] 
    public string Code { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<RuleEntry> RuleEntries { get; set; } 


} 

我想與所有的RuleEntry數據,但在相應的外鍵名稱的表格,此刻只有密鑰節目。 我的電網代碼如下:

@(Html.Kendo().Grid(Model)  
.Name("Grid") 
.Columns(columns => 
{ 
    columns.Bound(p => p.Country); 
    columns.Bound(p => p.Family); 
    columns.Bound(p => p.IP); 
    columns.Bound(p => p.RuleKey); 
    columns.Bound(p => p.Status); 
    columns.Bound(p => p.Title); 

}) 
.Groupable() 
.Sortable() 
.Scrollable(s => s.Height("auto")) 
.Filterable() 
.ColumnMenu()) 

我該怎麼做?在模型,控制器或視圖中?

感謝

回答

2

在RuleEntry類,增加對國家1 ForeignKey的屬性如下:

[ForeignKey("Country")] 
public virtual Country Country1 { get; set; } 

在網格中,使用columns.Bound(p => p.Country1.Name);

+0

謝謝!奇蹟般有效 :) – 2013-04-08 09:59:43