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

Two foreign key drop-downs in grid?

3 Answers 309 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Magnus
Top achievements
Rank 1
Magnus asked on 13 Sep 2016, 10:40 PM

Hi! I use MVC Core and Kendo UI for MVC.

I have a grid that has two different foreign key columns, for which I want drop-downs. I have two problems:
1. How can I bind any of them to the MVC Model? TheGridForeignKey.cshtml that I copied in to the Shared/EditorTemplates folder uses some unknown field in the ViewData (which is null and throws an exception at run-time). I want to use MVC. I tried the following which hard-codes some values but it at least shows the drop-down in grid both columns:

Html.Kendo().DropDownListFor(m => m)
       .BindTo((SelectList) new SelectList(new List<string> { "A", "B", "C" }) )

2.The GridForeignKey.cshtml is shared for all views in the whole site, how can I bind it in the actual view instead, where I have control over data?

As you can see, I haven't really figured out how all this works and is glued together. 

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 15 Sep 2016, 12:35 PM
Hello Peter,

As you can see in the following online demo, the ForeignKeyColumn is populated with a collection from the ViewData, which in other hand is set to the ViewData within the control and the PopulateCategories method:
columns.ForeignKey(p => p.CategoryID, (System.Collections.IEnumerable)ViewData["categories"], "CategoryID", "CategoryName").Title("Category").Width(150);

And the PopulateCategories method:
private void PopulateCategories()
{
    var dataContext = new SampleEntities();
    var categories = dataContext.Categories
                .Select(c => new CategoryViewModel {
                    CategoryID = c.CategoryID,
                    CategoryName = c.CategoryName
                })
                .OrderBy(e => e.CategoryName);
 
    ViewData["categories"] = categories;

Hope this helps.


Regards,
Konstantin Dikov
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Magnus
Top achievements
Rank 1
answered on 15 Sep 2016, 01:16 PM

Hi!

1. I saw the demo with ViewData, but isn't there a way to use the MVC model? The data is already in there.

2. The Html.Kendo().DropDownListFor(m => m).BindTo(....) which is copied to the Shared/EditorTemplates, binds to one object. I need two different drop-downs with different (and unrelated) data.

3. The .BindTo() that is in the Shared/EditorTemplates does not work as is, it binds to
(SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"]
which obviously doesn't work. I don't understand why the template must bind to data? And if the template binds to data, how can can I have more than 1 drop-down in my whole project? (since I have to place it in the Shared folder)

I must be missing something fundamental here!

0
Konstantin Dikov
Telerik team
answered on 16 Sep 2016, 08:32 AM
Hello Peter,

I am not sure that I understand the exact question. If you have the values in the model, there will be no need for using the ForeignKeyColumn, because its idea is to match the values of the Grid records particular field to another table. Please note that you can use custom editor templates and create your own custom logic for the editors if the ForeignKeyColumn does not match your requirements:
As for the ForeignKeyColumn editor template, note that the data that is bound to the DropDownList is set to the ViewData from the Column's definition and it and it is mandatory to provide the data in the template.

Regarding the multiply ForeignKeyColumn, once you set different data in the columns definition, the template will use that particular data when it initializes the editor.


Regards,
Konstantin Dikov
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Magnus
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Magnus
Top achievements
Rank 1
Share this question
or