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

Display Child Object Data in a ForeignKey Column?

3 Answers 225 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 05 Mar 2013, 08:06 PM
Title says it all. I'd like to accomplish what's being done on this page:
http://demos.kendoui.com/web/grid/editing-custom.html

Specifically:
columns.Bound(p => p.Employee).ClientTemplate("#=Employee.EmployeeName#");

But my column is a ForeignKey column and my child object seems to come back null. Any ideas how I could get this done?

Thank you!
Michael

3 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 07 Mar 2013, 03:25 PM
Hi Michael,

 

Basically to allow editing of nullable field in current scenario you should make the following changes:

  •  Modify the ClientTemplate as follows:
columns.Bound(p => p.Employee).ClientTemplate("#=Employee ? Employee.EmployeeName : ''#");
  • Include the values for the DropDownList in the ViewData:
var employees = dataContext.Employees
            .Select(c => new ClientEmployeeViewModel {
                EmployeeID = c.EmployeeID,
                EmployeeName = c.EmployeeName
            })
            .OrderBy(e => e.EmployeeName);
ViewData["employees"] = employees;
  • Create custom editor template for the "Employee" property:
@model Kendo.Mvc.Examples.Models.ClientEmployeeViewModel
 
@(Html.Kendo().DropDownListFor(m => m)
        .DataValueField("EmployeeID")
        .DataTextField("EmployeeName")
        .OptionLabel("Please select ...")
        .BindTo((System.Collections.IEnumerable)ViewData["employees"])
)
  • Define default value for the "Employee" property:
model.Field(p => p.Employee).DefaultValue(new Kendo.Mvc.Examples.Models.ClientEmployeeViewModel() { EmployeeID = 1, EmployeeName = "Andrew" });


Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Michael
Top achievements
Rank 1
answered on 07 Mar 2013, 06:31 PM
The client object should NOT be null, though. It has something in it but it's not there for me to use for some reason. Basically this grid relies on a table that has a foreign key reference. When using the GirdForeignKey it works fine right up until I have '#' (pound signs) in the Text Display Field. So I need to be able to use a ClientTemplate to call a function that replaces the #'s with \\#'s, but I can't seem to grab that text to pass. Make ANY sense?
0
Vladimir Iliev
Telerik team
answered on 11 Mar 2013, 01:29 PM
Hi Michael,

 
From the provided information it's not clear for us what exactly you are trying to achieve - the example that I provide with my last reply works as expected when the text contains "#" symbol. Also the example provide the same functionality as ForeignKeyColumn and additionally supports editing the data from the child object. 

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Michael
Top achievements
Rank 1
Share this question
or