Hello. I am trying to implement a dropdownlist and I need some help. I have 2 simple tables (Employee and Department). Each employee belongs to a department and a department can have multiple employees. I am using EF database first approach. EF generated the 2 classes (Employee.cs and Department.cs) and each class has a reference to the other.
This is my controller actionmethod
public ActionResult Kendo()
{
db.Configuration.LazyLoadingEnabled = false;
var departments = db.Departments.ToList();
ViewData["departments"] = departments;
var employees = db.Employees.Include(e => e.Department).ToList();
return View(employees);
}
The view is
@(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(c =>
{
c.Bound(e => e.EmployeeId);
c.Bound(e => e.FirstName);
c.Bound(e => e.LastName);
c.Bound(e => e.Gender);
c.Bound(e => e.City);
c.Bound(e => e.Department.DepartmentName);
//columns.ForeignKey(e => e.DepartmentId, Model.Select(e => e.Department), "DepartmentId", "DepartmentName");
//columns.Bound(e => e.Department.DepartmentName).EditorTemplateName("DepartmentEditor").Title("Department").Width(250);
//columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
})
//.DataSource(d => d
// .Ajax()
// .ServerOperation(false)
//)
.Pageable() //Enable the paging.
.Sortable() //Enable the sorting.
.Filterable()
.Scrollable()
)
The EditorTemplate
@(Html.Kendo().DropDownList()
.Name("Department")
.DataValueField("DepartmentId")
.DataTextField("DepartmentName")
.BindTo((IEnumerable)ViewData["departments"])
)
Issues I am having:
- I am getting a circular reference if I uncomment the datasource line in the view.
- I need to have CRUD operations and I need to show the department as a dropdownlist on Edit/Add. On Edit, the dropdownlist needs to select the current department of the employee.
Thanks.