Foreign Key column not loading DropDownLoad in Edit Mode (Kendo.Mvc v.2013.3.1119.545)

7 posts, 1 answers
  1. G
    G avatar
    4 posts
    Member since:
    Feb 2014

    Posted 06 Feb 2014 Link to this post

    Currently I have a grid that I am trying to have a drop down list appear in one of the columns while in Edit mode using the ForeignKey column.

    @(Html.Kendo().Grid<Inspection>()
                    .Name("grid")
                    .DataSource(dataSource => dataSource.Ajax()
                    .Model(model =>
                    {
                        model.Id(p => p.ID);
                        model.Field(p => p.Date);
                        model.Field(p => p.TypeID);
                    })
                     .Read(read => read.Action("grid_Read", "Grid", new { ID = Model.ID }))
                    .Update(update => update.Action("grid_Update", "Grid", new { ID = Model.ID }))
                    .PageSize(10))
                    .Columns(columns =>
                    {
                        columns.Bound(c => c.RequestedDate).Format("{0:d}").EditorTemplateName("_DateTime").Width(200);
                        columns.ForeignKey(c => c.TypeID, (System.Collections.IEnumerable)ViewBag.Types, "TypeID", "TypeName").Title("Test Types");
     
                        columns.Command(command => { command.Edit(); });
                    })
                    .Editable(editable => editable.Mode(GridEditMode.InLine))
                    .Pageable(pageable => pageable.Messages(messages => messages.Empty("Nothing Here")))
                    .Sortable(sortable => sortable.AllowUnsort(false))
                )


    The grid loads with all the correct data, and even shows the TypeName name while in Read mode. However, when switching to Edit mode the column shows the TypeID in a text box, not a drop down list. The code to populate the ViewBag is below

    ViewBag.Types = _service.Repository.InspectionTypes;


    All the data appears to end up on the page - when inspecting the editable element the list of options appears in a jQuery script, however, they do not populate a drop down while editing.

    The code matches the Kendo MVC demo as much as possible, so am I missing something? Are there certain constraints that need to be met?
  2. Answer
    Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 10 Feb 2014 Link to this post

    Hi,

    Most probably there is some JavaScript error on the page which prevents the DropDownList from initializing. For convenience I created small ForeignKeyColumn example and attached it to the current thread - could you please modify it to reproduce the issue and send it back to us? This would help us pinpoint the exact reason for this behavior.

    Kind Regards,
    Vladimir Iliev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. G
    G avatar
    4 posts
    Member since:
    Feb 2014

    Posted 10 Feb 2014 in reply to Vladimir Iliev Link to this post

    Vladimir,

    Thank you for the solution, by providing all of the code necessary to use the ForeignKey column properly, I was able to get my solution working. You should add the GridForeignKey.cshtml file to the demonstration code on your website, seeing as the code in there is what actually allows the ForeignKey column to display as a dropdownlist and not just a textbox.

    In hopes of helping other people searching for the solution, below is the code for GridForeignKey.cshtml

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

    Create a file named GridForeignKey.cshtml and place it in the "Shared" folder for the MVC application. This file is what actually makes the dropdownlist appear in the grid when editing.

    Thank you,

    G
  5. Mayuri
    Mayuri avatar
    7 posts
    Member since:
    Sep 2015

    Posted 22 Sep 2015 in reply to G Link to this post

    Hi,

    I did it exactly in the same way but still in my case dropdown is not appearing in grid. When clicked on the cell it turns into a textbox with cross mark to the right with no values. Am I missing anything? I have created a GridForeignKey.cshtml in Shared -> EditorTemplates folder.

    I also tried it by having simple Columns.Bound method and by attaching EditorTemplateName. In this method cell turns into dropdown with values in it but when I select any option from the list and click outside the cell then dropdown disappears and selected options value gets displayed in the cell. I want to display the description instead of value.

    Can you please suggest a workable solution for me. What are the reuirements/specifications for using ForeignKeyColumn method? Its urgent. I have spent lot of time on this issue but no success.

    Thanks.

     

     

     

  6. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 24 Sep 2015 Link to this post

    Hello Mayuri,

    From the provided information it's not clear for us why the ForeignKeyColumn is not finding it's editor template - could you please check the model field is not having "UIHint" attribute over it as well as if the "EditorTemplates" folder is placed correctly? If this is not the case could you please provide runable sample where the issue is reproduced for further investigation on our side? 

    Also for more information about the editor templates you can check this help article.

    Regards,
    Vladimir Iliev
    Telerik
    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
  7. Mayuri
    Mayuri avatar
    7 posts
    Member since:
    Sep 2015

    Posted 24 Sep 2015 in reply to Vladimir Iliev Link to this post

    Vladimir,

     

    Thanks for your reply.

    Yes the field is having UIHint attribute set. I tried it both way by having it and by removing it but no difference. One point to let  you know is my ViewModel is in a Web API project which is another application referenced in my MVC application. Also I have one question: to use ForeignKey column for dropdown fields, does it need to have a Primary Key-Foreign key relation set on the table? because in my case this relationship is not set on table. There are some lookup tables whose values are used in main table.

    Also I wanted to use Enums collection for these fields. Is it possible to bind Enum values for dropdown with ForeignKeyColumn in grid?

     

    Thanks.

     

  8. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 28 Sep 2015 Link to this post

    Hi,

    As this thread is getting out of the original topic, may I kindly ask you to open a new support thread for the Grid and provide runable example where the editor template is not loaded correctly? In this way it is much easier to follow and concentrate on the particular issue which usually leads to its faster resolving.

    Regards,
    Vladimir Iliev
    Telerik
    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