This is a migrated thread and some comments may be shown as answers.

MVC Grid Dropdownlist help

1 Answer 322 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Multi User Account
Top achievements
Rank 1
Multi User Account asked on 18 Dec 2018, 02:44 PM

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.

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 20 Dec 2018, 08:12 AM
Hello Walter,

I have examined the provided configuration and noticed that the grid is using server binding and you are attempting to add an AJAX dataSource. Using both server binding and ajax binding is not possible due to their internal workflow. When using server binding, the actual html table is generated on the server. However, with ajax binding the data is fetched via an ajax request from the client and the table is generated client side.

With that said I would recommend you to use either of the approaches.

Furthermore, regarding the circular reference issue. I would recommend you to take a look at the following article which demonstrates three different solutions:


Finally, in the link below, you will find an article which explains how to create a drop down editor step by step:



Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Multi User Account
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Share this question
or