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!