0

我有一个包含许多外键列的网格。现在只有键显示,但我想要这个例子中的正确数据。

以下代码是我填充网格的内容:

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

    [ForeignKey("Country")]
    internal virtual Country Country1 { get; set; }
    [ForeignKey("Family")]
    internal virtual Family Family1 { get; set; }
    [ForeignKey("IP")]
    internal virtual IP IP1 { get; set; }
    [ForeignKey("Status")]
    internal virtual RuleStatus RuleStatus { get; set; }
    [ScriptIgnore]
    internal virtual ICollection<RuleEntriesCas> RuleEntriesCases { get; set; }
}

模型上下文如下所示:

public partial class entitiesName: DbContext
{
    public entitiesName()
        : base("name=entitiesName")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Case> Cases { get; set; }
    public DbSet<Country> Countries { get; set; }
    public DbSet<Family> Families { get; set; }
    public DbSet<IP> IPs { get; set; }
    public DbSet<RuleEntry> RuleEntries { get; set; }
    public DbSet<RuleEntriesCas> RuleEntriesCases { get; set; }
    public DbSet<Rule> Rules { get; set; }
    public DbSet<RuleStatus> RuleStatuses { get; set; }
}

我的 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; }       
}

我的cshtml文件看起来像这样

@(Html.Kendo().Grid(Model)    
.Name("Grid")
.Columns(columns =>
{

    columns.ForeignKey(r => r.Country, (System.Collections.IEnumerable)ViewData["Countries"], "Code", "Name")
        .Title("Country").Width(150);
    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()
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Sortable()
.Scrollable(s => s.Height("auto"))
.Filterable()
.ColumnMenu()
.DataSource(dataSource => dataSource
        .Ajax()
        .Batch(true)
        .ServerOperation(false)
        .Model(model => model.Id(p => p.ID))
    ))

但是 columns.ForeignKey-line 会产生难以调试的错误

值不能为空。参数名称:items

我不知道如何解决这个问题,谷歌也不会给我任何相关的答案。谢谢!

4

1 回答 1

0

您需要指定外键的id ,而不是导航属性:

这应该解决它:

columns.ForeignKey(
  r => r.Country.Code,
  ( IEnumerable ) ViewData["Countries"],
  "Code",
  "Name"
);

它使用了这个重载GridColumnFactory<TModel>.ForeignKey()

public virtual GridBoundColumnBuilder<TModel> ForeignKey<TValue>(
  Expression<Func<TModel, TValue>> expression,
  IEnumerable data,
  string dataFieldValue,
  string dataFieldText
;
于 2013-04-08T15:13:35.350 回答