0

I'm having a grid with numerous foreign key columns. Right now only the key shows but I want the correct data like in this example.

Following code is what I populate the grid with:

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

Model context looks like this:

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

My Country class looks like this:

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

And my cshtml file looks like this

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

However the columns.ForeignKey-line generates the hard-to-debug error

Value cannot be null. Parameter name: items

I have no clue how to figure out the problem, Google wont give me any relevant answers either. Thanks!

Oskar Eriksson
  • 845
  • 1
  • 9
  • 28

1 Answers1

0

You need to specify the id of the foreign key, not the navigation property:

This should fix it:

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

Which uses this overload of GridColumnFactory<TModel>.ForeignKey()

public virtual GridBoundColumnBuilder<TModel> ForeignKey<TValue>(
  Expression<Func<TModel, TValue>> expression,
  IEnumerable data,
  string dataFieldValue,
  string dataFieldText
;
Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • Thanks for your answer, I'm afraid this gives an error: "Cannot convert lambda expression to type 'string' because it is not a delegate type". Any idea how to get around this? – Oskar Eriksson Apr 09 '13 at 06:16
  • Thanks for the update, but I'm still getting the same lambda expression error. The only difference between mine and yours code is that I got "System.Collections.IEnumerable", that should be the same thing? – Oskar Eriksson Apr 09 '13 at 14:30
  • Yes, that should work - confusing! Is your `Model` of type `IEnumerable`? – Nick Butler Apr 09 '13 at 14:42
  • No, it's DbSet. Tried to change them to IEnumerable but that generated alot of errors. Added my model code aswell. – Oskar Eriksson Apr 09 '13 at 15:00
  • That should be ok - `DbSet` implements `IEnumerable`. Can you check your code carefully? I can't see why you get that error. – Nick Butler Apr 09 '13 at 17:11