I am trying to implement a grid with a foreign key Column.
I am using your example
http://demos.telerik.com/aspnet-core/grid/foreignkeycolumn
I am trying to understand the right way to implement the editor template, unfortunately you didn't provide a full working example.
this is the error:
An unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: items
.ctor
And this is the view
@(Html.Kendo().Grid<TraderMade.Models.Market>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.Name).Width(200);
columns.ForeignKey(p => p.CountryId, (System.Collections.IEnumerable)ViewData["countries"], "Id", "Name")
.Title("Nazione").Width(150);
columns.Bound(c => c.City).Title("Città ").Width(150);
columns.Bound(c => c.MIC).Title("Codice").Width(80);
columns.Bound(c => c.WebSiteUrl).Title("Web site").Width(250);
columns.Command(command => command.Destroy()).Width(110);
})
.Filterable(ftb => ftb.Mode(GridFilterMode.Menu))
.HtmlAttributes(new { style = "height: 380px;" })
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Scrollable()
.Groupable()
.Sortable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.Id))
.Create("Countries_Create", "Home")
.Read("Countries_Read", "Home")
.Update("Countries_Update", "Home")
.Destroy("Countries_Delete", "Home")
).Deferred()
)
this is the relevant part of the controller
private void PopulateCountries()
{
var countries = _context.Countries.ToList();
ViewData["countries"] = countries;
ViewData["defaultCountry"] = countries.First();
}
public IActionResult Index()
{
PopulateCountries();
return View();
}
public ActionResult Countries_Read([DataSourceRequest] DataSourceRequest request)
{
var markets = _context.Markets;
DataSourceResult result = markets.ToDataSourceResult(request);
return Json(result);
}
I have read somewhere that I need to create a folder in views\shared\EditorTemplates, in this folder I have created
GridForeignKey.cshtml
@model object
@(
Html.Kendo().DropDownListFor(m => m)
.BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
.ValuePrimitive(true)
)
here the model:
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
public string CodeAlpha2 { get; set; }
public string CodeAlpha3 { get; set; }
public int CodeNumeric { get; set; }
public ICollection<Market> Markets { get; set; }
}
public class Market
{
public int Id { get; set; }
public string MIC { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string WebSiteUrl { get; set; }
public string Notes { get; set; }
public bool IsEnabled { get; set; }
public int CountryId { get; set; }
[ForeignKey("CountryId")]
public Country Country { get; set; }
}
I have tried to use ViewModel instead of model, but no matter what i write in columns.ForeignKey I have always the same error.
thank you for the support