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

Binding ForeignKey to an object other than SelectList

2 Answers 195 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nicholas
Top achievements
Rank 1
Nicholas asked on 17 Jul 2014, 09:51 PM
Hi,

I'm wondering if it's possible to bind the ForeignKey column builder to a collection other than MVC's SelectList (for example, one with items containing properties other than just Id and Text).

In the regular DropDownListBuilder, there is an overload of BindTo() that takes an IEnumerable that allows you to do this, but from looking at the source code, all of the ForeignKey overloads for IEnumerable just convert it to a SelectList internally. My reason for wanting this is that we frequently have cases where the data in the ForeignKey needs an additional ID property for some related entity that we use for filtering the data source later.

The only way I've found to solve this so far is to use a Bound column instead and specify an editor template that uses a DropDownList with the BindTo overload that takes an IEnumerable. The problem with this is that the column will only display the integer ID outside of edit mode -- using ForeignKey will do the ID -> text mappings automatically in either mode. Is there a better way to do this?

Thanks,
Nick

2 Answers, 1 is accepted

Sort by
0
Accepted
Vladimir Iliev
Telerik team
answered on 21 Jul 2014, 11:35 AM
Hi Nicholas,

Basically when you pass field from the "ViewData" which contains IEnumerable collection to the ForeignKeyColumn builder it will create new collection under the "FIELDNAME_Data" field which is passed to the editor, however the original collection is not removed and you can pass it to the DropDownList (the other collection will be used only for matching the column values to their texts in display mode). Please consider the following example:

- Controller:
ViewData["VendorId_custom"] =  vendorList.Select(e => new
        {
            VendorId = e.VendorId.ToString(),
            VendorName = e.VendorName,
            //additional field which will be available in the editor
            CustomerId = e.CustomerId
        });

- Column definition:
//Set the editor template name
columns.ForeignKey(p => p.VendorId, (System.Collections.IEnumerable)ViewData["VendorId_custom"], "VendorId", "VendorName")
    .HeaderHtmlAttributes(new { @title = "Vendor" })
    .Width(300)
    .EditorTemplateName("VendorId");

- EditorTemplate:
@(Html.Kendo().DropDownListFor(m => m)
    .AutoBind(false)
    .OptionLabel("Select Vendor...")
    .DataTextField("VendorName")
    .DataValueField("VendorId")
    .BindTo((System.Collections.IEnumerable)ViewData["VendorId_custom"])
)

Using the above configuration, the DropDownList items will contain additional field - "CustomerId".

Regards,
Vladimir Iliev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Nicholas
Top achievements
Rank 1
answered on 24 Jul 2014, 02:50 PM
Thanks Vladimir, that solved the problem.
Tags
Grid
Asked by
Nicholas
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Nicholas
Top achievements
Rank 1
Share this question
or