I've read the examples, and I've followed along as best as I can, but I'm still having an issue getting a simple dropdown into my grid (ASP.NET MVC)
I have a model called Supplier, and a model called VehicleType
public class Supplier
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Name = "Id")]
public int SupplierId { get; set; }
[Display(Name = "Supplier Number")]
public int SupplierNumber { get; set; }
[Display(Name = "Name")]
public string SupplierName { get; set; }
[Display(Name = "Vehicle Type Id")]
public int VehicleTypeId { get; set; }
[Display(Name = "Status Id")]
public int StatusId { get; set; }
// I added this
[NotMapped]
[UIHint("VehicleType")]
public VehicleType VehicleType { get; set; }
}
public class VehicleType
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VehicleTypeId { get; set; }
public string VehicleTypeName { get; set; }
}
My Grid is as follows:
@(Html.Kendo().Grid<MyProject.Models.Schedule.Supplier>
()
.Name("grid")
.Columns(columns =>
{
columns.Select().Width(50);
columns.Bound(p => p.SupplierNumber).Filterable(ftb => ftb.Multi(true));
columns.Bound(p => p.SupplierName).Groupable(true).Filterable(ftb => ftb.Multi(true));
//columns.Bound(p => p.VehicleTypeId).Filterable(ftb => ftb.Multi(true));
columns.Bound(p => p.VehicleType).ClientTemplate("#=VehicleType.VehicleTypeName#").Sortable(false);
})
.Pageable()
.Sortable()
.ToolBar(toolBar =>
{
toolBar.Create();
toolBar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.PersistSelection()
.Scrollable()
.Groupable()
.Filterable()
.HtmlAttributes(new { style = "height:600px;" })
.Resizable(r => r.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(p => p.SupplierId))
.PageSize(100)
.Events(events => events.Error("error_handler"))
.Read(read => read.Action("GetSuppliers", "Schedule"))
.Update(update => update.Action("EditSupplier", "Schedule"))
.Create(create => create.Action("CreateSupplier", "Schedule"))
)
)
My controller looks like this:
// View for the page
public ActionResult Supplier()
{
ViewData["vehicleTypes"] = new Models.MyContext()
.vehicleTypes
.Select(v => new VehicleType
{
VehicleTypeId = v.VehicleTypeId,
VehicleTypeName = v.VehicleTypeName
})
.OrderBy(v => v.VehicleTypeName);
return View();
}
// Using this to get the data for the grid
public ActionResult GetSuppliers([DataSourceRequest] DataSourceRequest request)
{
List<Models.Schedule.Supplier> result = MyLogic.suppliers();
return Json(result.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
And finally I created an EditorTemplate called VehicleType.cshtml
@(Html.Kendo().DropDownList() .Name("VehicleType") .DataValueField("VehicleTypeId") .DataTextField("VehicleTypeName") .BindTo((System.Collections.IEnumerable)ViewData["vehicleTypes"]) )
When I try to view the grid I get this error:
The entity or complex type 'MyProject.Models.VehicleType' cannot be constructed in a LINQ to Entities query.
After some googling, I changed my ViewData lookup in my controller to this:
Which displays my grid (empty though there should be 31 rows) and the console has the following error:
Uncaught TypeError: Cannot read properties of null (reading 'VehicleTypeName')
at eval (eval at compile (kendo.all.js:241:30), <anonymous>:3:3673)
at init._rowsHtml (kendo.all.js:74650:37)
at init._renderContent (kendo.all.js:75513:34)
at init.refresh (kendo.all.js:75329:22)
at init.d (jquery.min.js:2:3873)
at init.trigger (kendo.all.js:171:37)
at init._process (kendo.all.js:8341:22)
at init.success (kendo.all.js:8037:22)
at success (kendo.all.js:7928:42)
at Object.n.success (kendo.all.js:6762:25)
Can someone point me in the right direction here?
Without trying to add the dropdown, it all works as expected, but I'd rather not have to type in the id values when editing/creating a row
I've been bashing my head against the wall for it for a couple of hours now, so any kind of help would be greatly appreciated.
Thanks