值不能为空。参数名称:items。在Kendo UI Grid中查看foreignkey

时间:2013-04-08 15:04:41

标签: asp.net-mvc kendo-ui kendo-grid argumentnullexception

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

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

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

我的国家课程如下:

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

我不知道如何找出问题,谷歌也不会给我任何相关的答案。谢谢!

1 个答案:

答案 0 :(得分: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
;