asp.net-mvc - 在 Kendo UI 网格中显示外部数据

标签 asp.net-mvc entity-framework kendo-ui kendo-grid

使用 Kendo UI 获得了 MVC4、 Entity Framework 数据库第一个项目。

我在网格中显示的模型如下所示:

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”,并且是包含名称“Sweden”的表“Country”的外键。国家/地区模型如下所示:

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())

我该怎么做?在模型、 Controller 或 View 中?

谢谢

最佳答案

在RuleEntry类中,为Country1添加ForeignKey属性,如下所示:

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

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

关于asp.net-mvc - 在 Kendo UI 网格中显示外部数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875344/

相关文章:

asp.net-mvc - 如何设置默认值 HTML.TextBoxFor()

c# - 将 Entity Framework Core 实体转换为 SQL 字符串

c# - 使用 EntityFramework 在 MVC 中以 JSON 形式返回具有导航属性的实体的最佳实践

sorting - 剑道网格服务器端过滤并且不工作

c# - 在同一线程中类的所有实例之间共享类的数据成员

asp.net-mvc - 抛出异常或引发领域事件?

c# - 使用经度和纬度查找给定距离内的所有附近客户

javascript - 来自网格的列数组的 Kendo UI 动态详细信息模板

kendo-ui - Kendo Grid 列的 ClientTemplate 问题

asp.net-mvc - 使用模型中的文本渲染跨度元素