Using a ClientTemplate on a ForeignKey in a Grid

1 Answer 70 Views
Grid
Chris
Top achievements
Rank 1
Iron
Iron
Iron
Chris asked on 01 Nov 2024, 10:47 AM

Hi

I have a grid that has a foreign key to lookup and display the value from another table. I want to make the column into a hyperlink when not in edit mode, to go and view the related record. I can't seem to get the textual value to display, it's fine obviously with the numerical FK value.

My code is :


.Columns(columns =>
{
    columns.ForeignKey(p => p.PrimaryKeyId, ds => ds.Read(r => r.Action("CustomerList", "Customer")), "CustomerId", "CustomerName")
        .Title("Company")
        .ClientTemplate("<a href='/Customer/Edit/#=data.PrimaryKeyId#' class='link-primary'>#=data.Customer.CustomerName#</a>")
        .HeaderHtmlAttributes(new { @class = "small" });
    columns.Bound(c => c.Primary).HeaderHtmlAttributes(new { @class = "small" });
    columns.ForeignKey(p => p.SiteId, ds => ds.Read(r => r.Action("xCustomerSitesJSon", "Customer", new { custId = 0 })), "SiteId", "SiteName")
        .Title("Site")
        .HeaderHtmlAttributes(new { @class = "small" });
    columns.Command(command =>
    {
        command.Edit();
    });
})

Obviously it is the #=data.Customer.CustomerName# part that doesn't work - I understand that the Customer isn't really part of the data array, but not sure if / how this can be accessed?

Thanks

1 Answer, 1 is accepted

Sort by
0
Ivaylo
Telerik team
answered on 01 Nov 2024, 03:03 PM

Hello Chris,

Thank you for the details provided.

To ensure the code functions correctly, the "Grid" model should include two essential properties: a foreign key and a navigation property that references the related object. 

public class GridModel
{
    //other properties

    public int CustomerId { get; set; }

    public Customer Customer { get; set; }
}

If you require additional information about the "ForeignKey" in the "Grid", here I will provide some resources you can review:

In addition, I prepared a sample REPL using the demo I provided above.

The code can be tested and observed.

I hope this information was helpful.

Regards,
Ivaylo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Chris
Top achievements
Rank 1
Iron
Iron
Iron
commented on 01 Nov 2024, 04:18 PM

Thanks - it's what I would usually do if creating my own tables - was a bit confused because using the ForeignKey without the navigation property worked and displayed the name fine, it was only when I needed to manually control it using the ClientTemplate it was an issue.
Ivaylo
Telerik team
commented on 04 Nov 2024, 02:48 PM

Hi Chris,

I am glad to hear that now the issue is resolved and hope my assistance was valuable.

Feel free to contact me and the Team if additional information or assistance is needed.

Best Regards,

Ivaylo

Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Ivaylo
Telerik team
Share this question
or