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

Grid doesn't create DropDownList

1 Answer 302 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 07 Jun 2018, 10:19 PM

I am evaluating your software for purchase and have encountered the following problem. I am trying to create a grid which will have a dropdown list editor for one of its columns. According to the comment in the thread https://www.telerik.com/forums/grid-with-drop-down-not-working it should be sufficient to create a foreign key column and then provide the data necessary to populate the dropdown. This isn't working for me. The editor for the personid column should be a dropdown list, but it isn't. No errors are shown when the project is built nor when the web page is displayed. Here is my code:

ScheduleGrid.cshtml

@{
    ViewData["Title"] = "Schedule";
}
<h2>Schedule</h2>

@(Html.Kendo().Grid<SessionItemModel>()
      .Name("ScheduleGrid")
      .DataSource(d => d
         .Ajax()
         .Batch(true)
         .PageSize(7)
         .ServerOperation(false)
         .Model(model => { model.Id(p => p.pkey);  })
         .Read(r => r.Action("Read", "Schedule"))
         .Update(u => u.Action("Update","Schedule"))
         .Group(g => g.Add(p => p.daypart)))
      .Columns(c => {
         c.Bound(m => m.pkey).Visible(false);
         c.Bound(m => m.sessionkey).Visible(false);
         c.Bound(m => m.daypart).Title("Day Part").Visible(false);
         c.Bound(m => m.type).Visible(false);
         c.Bound(m => m.slot).Title("Slot");
         c.ForeignKey(m => m.personid, (System.Collections.IEnumerable)ViewData["persons"], "personid", "name").Title("Name");
         c.Bound(m => m.editable).Visible(false);
      })
      .Groupable(false)
      .Pageable()
      .Editable(e => e.Mode(GridEditMode.InCell))
      )

ScheduleController.cs

   public class ScheduleController : Controller {
      private sessionsRepository repo = new sessionsRepository();

      public IActionResult Index() {
         ViewData["persons"] = repo.GetPersons();
         return View("ScheduleGrid");
         }

      public ActionResult Read([DataSourceRequest] DataSourceRequest request) {
         List<SessionItemModel> sessions = repo.GetItems();
         DataSourceResult result = sessions.ToDataSourceResult(request);
         return Json(result);
         }

      [HttpPost]
      public ActionResult Update([DataSourceRequest] DataSourceRequest request,  [Bind(Prefix = "models")]IEnumerable<SessionItemModel> items) {
         // update sessions here

         List<SessionItemModel> sessions = repo.GetItems();
         DataSourceResult result = sessions.ToDataSourceResult(request);

         return Json(result);
         }
      }

 

 

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 08 Jun 2018, 01:25 PM
Hello Tom,

The Grid component does not create editors for the fields automatically. By default it would look for editors located in the ~/Views/Shared/EditorTemplates folder. The folder can contain multiple partial views each one named like a primitive type. Also the foreign key editor will be there and it would be called GridForeignKey.cshtml. The editor used in the ForeignKey example looks like in the code snippet.

@model object
            
@(
 Html.Kendo().DropDownListFor(m => m)       
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
)


If you would like information on specifying editors for the Grid you would find the article below interesting.



Regards,
Viktor Tachev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Tom
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or