Two foreign key drop-downs in grid?

4 posts, 0 answers
  1. Peter
    Peter avatar
    8 posts
    Member since:
    Mar 2016

    Posted 13 Sep Link to this post

    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. 

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1796 posts

    Posted 15 Sep Link to this post

    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
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Peter
    Peter avatar
    8 posts
    Member since:
    Mar 2016

    Posted 15 Sep in reply to Konstantin Dikov Link to this post

    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!

  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1796 posts

    Posted 16 Sep Link to this post

    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
Back to Top
UI for ASP.NET MVC is VS 2017 Ready