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

Editable Dropdown in Grid Populated from ViewBag

2 Answers 343 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 2
Iron
Brian asked on 05 Jun 2019, 05:04 PM

Hi,

https://demos.telerik.com/aspnet-mvc/grid/editing-custom

The above link discusses adding a dropdown in editable mode to select from a list.  What if I don't want the list to come from the model?  I'm trying to bind the dropdown to a ViewBag of names populated from an LDAP query (ie not the database).  What do I put in the ClientTemplate section?

Thanks,
Brian

2 Answers, 1 is accepted

Sort by
0
Brian
Top achievements
Rank 2
Iron
answered on 07 Jun 2019, 01:36 PM
I resolved this by using the Foreign Key column instead.
0
Accepted
Tsvetomir
Telerik team
answered on 10 Jun 2019, 11:51 AM
Hi Brian,

Thank you for sharing with the community the approach that you have undertaken given the current circumstances.

In the live demo the model has been explicitly applied at the top so that the editor could be set-up according to that model. However, the data comes from the ViewData and not from the model itself. It is done via the BindTo() property:

@model Kendo.Mvc.Examples.Models.CategoryViewModel
 
@(Html.Kendo().DropDownListFor(m => m)
        .DataValueField("CategoryID")
        .DataTextField("CategoryName")
        .BindTo((System.Collections.IEnumerable)ViewData["categories"])
)

Here, you would be able to make use of the ViewBag as well. As per the client template, in the context of the grid, its column have to be bound to flat data fields. If they are bound to a complex object, explicitly the property of the object has to be set to specify what text has to be shown in the column. 

columns.Bound(p => p.Category).ClientTemplate("#=Category.CategoryName#").Sortable(false).Width(180);

In this example, the Category has a field called CategoryName which should be used to display the correct text. 

Another approach which you might undertake is to make use of the ForeignKey column. It uses an editor template which is a dropdown-list and its functionality is similar to the custom editors.

In case any questions arise, let me know.


Kind regards,
Tsvetomir
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
Brian
Top achievements
Rank 2
Iron
Answers by
Brian
Top achievements
Rank 2
Iron
Tsvetomir
Telerik team
Share this question
or