Showing the value of a foreign key in Kendo UI Grid instead of just the ID

Currently in the grid, there is a drop-down list with foreign key values. However, when I select an item from the list, only the corresponding item ID is displayed instead of its name.

I attempted to follow the instructions outlined in this forum post, but all I am getting is 'undefined' in the grid row. Any suggestions or solutions?

Just a heads up, I am utilizing the free version of Kendo UI where everything operates in JavaScript.

Answer №1

In this version of MVC, a template is not required. Check out the following link for more details:

Suppose you want to display the Product name instead of using ProductID in your Orders table. You would utilize a column like this:

columns.ForeignKey(c => c.ProductID, (IEnumerable)ViewData["Products"], dataFieldText: "ProductName", dataFieldValue: "ProductID");

Make sure that your model contains the foreign key in the Orders model:

 [ForeignKey("ProductID")]
public Product FKProduct { get; set; }  

Update your controller accordingly:

public class HomeController : Controller {        
    private NorthwindRepository northwindRepository = new NorthwindRepository();
    public ActionResult Index()
    {
        PopulateProducts();         
        return View(northwindRepository.OrderDetails);     
    }
    private void PopulateProducts()     
    {         
        ViewData["Products"] = northwindRepository.Products;     
    }
} 

Answer №2

After seeking assistance from Telerik, I was able to find a solution that worked. You can view the solution in this JS Bin.

Answer №3

Utilizing a template is essential for this task.

Within your model, add another field that corresponds to the name (make sure to update it with the correct value), then implement a template similar to "#=name#

To begin, create your column in the following manner:

columns.Bound(x => x.ForeignKey).ClientTemplate("#=Name#");

In the Update method within your controller, assign the desired Name property to the view model.

viewModel.Name = GetName(viewModel.ForeignKey);
return this.Json(new[] { viewModel }.ToDataSourceResult(request, this.ModelState));

Update 2: When constructing the grid in JavaScript mode, define the column like so:

{ field: "ForeignKey", title: "Foreign Key", template: '#=Name#', editor: myEditor}

Then, specify your editor as follows:

function myEditor(container, options) {
    $('<input required data-text-field="ForeignKeyName" data-value-field="ForeignKey" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: false,
            dataSource: myDataSource
        });
}

You may still need to set the value accordingly. For server-side execution, follow the method mentioned above. If opting for client-side implementation, handle the grid save event and set the value in the data source there.

I hope this guidance proves beneficial.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Toggle the Editable Feature in AngularJS JSON Editor

Currently, I'm utilizing a library called ng-jsoneditor that is available on GitHub. I am attempting to switch the state of an element from being editable to being read-only. To indicate that an element should be read-only, I need to specify the onE ...

Trigger Angular Animation when there is a modification in the DOM element's appearance or styling

I've been working on implementing a fade-in animation in my Angular App that triggers every time the background changes, but I'm facing some challenges with it. Here's the relevant code snippet: HTML: <div @fadeIn [style.backgroundImag ...

Navbar toggle button in Bootstrap 4 does not function properly when placed within a button group

When the navbar is collapsed, I need two buttons to remain visible along with the toggle button. However, I don't want these buttons to collapse with the menu items. To address this issue, I placed the buttons inside a button group with the toggle but ...

Utilizing Regular Expressions in an Express.js Router

Looking for documentation on regex in Express has led me to discover that the information in the Express API is quite minimal. Currently, I am experimenting with a regex matching objectID example provided in the Express documentation. router.get(/^\/ ...

Issues with Callbacks Inquiry

I'm struggling to understand how to implement callbacks in this scenario. Despite researching and reading explanations, I can't seem to grasp the concept. Below is a snippet of my code: function retrieveTransactionInfo(transactionUrl) { re ...

Ways to identify the visible elements on a webpage using JavaScript

I'm working on a nextjs app , I am looking to dynamically update the active button in the navbar based on which section is currently visible on the page. The child elements of the page are structured like this: <div id="section1" > < ...

How to display server-side variables in an alert using JavaScript in ASP.NET

I need to display a server-side variable in a JavaScript alert. <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("iuser_id") %>' OnClick="deleteclick" onclientclick="return confirm('Are you sure you want ...

Guidelines for transforming an HTML string into a JavaScript document

Is there a simplified method to transform an HTML string into JavaScript code that generates the same markup using the DOM? Something similar to: Input <div class="foo" tabindex="4"> bar <button title="baz">bar ...

Bindings with Angular.js

I have developed an application similar to Pastebin. My goal is to allow users to paste code snippets and display them with syntax highlighting and other visual enhancements, regardless of the programming language used. To achieve this, I utilize Google&ap ...

Is it advisable to hold off until the document.onload event occurs?

I'm working with a basic HTML file where I need to generate SVGs based on data retrieved through an AJAX call. Do I need to ensure the document is fully loaded by enclosing my code within a document.onload = function() { ... } block, or can I assume ...

The synergy between HTML and JavaScript: A beginner's guide to frontend coding

I have primarily focused on server-side development of enterprise applications (Java EE, Spring framework). However, I am now exploring client-side technologies for better understanding (not necessarily to become an expert). I have studied HTML and CSS th ...

Cufon failing to load following an ajax call

At first, cufon is used to replace the text on the main page. However, when another page file is loaded, cufon does not apply its replacement to the newly loaded content. Why is that? I tried adding cufon.refresh(); as the last function in the chain. I c ...

Tips for displaying a div and highlighting the current link as active using jQuery after the page is reloaded

I am having trouble getting a div and link to stay active after the page reloads. The initial setup has the first link with an 'active' class and the div hidden. Despite checking everything I could think of, I can't seem to find a solution ...

Having trouble running npm start with Express generator

After installing Express Generator according to the instructions on the website, I encountered an issue when trying to run npm start. My Windows system has npm 6.4.1 and node v10.15.3 installed. > [email protected] start C:\Users\ONLINEW ...

Getting rid of items and bringing them back

In my current three.js project, I have implemented a feature that allows users to switch between scenes containing various mesh objects. All objects are loaded simultaneously when the page loads, but the performance is affected by the textures on these obj ...

The argument in question has not been defined

Experimenting with AngularJS, I encountered an error message in the browser console when trying to display a particular example: Error: Argument 'mainController as mainCtrl' is not a function, got undefined at Error (native) at bb My pr ...

The mystery of the unassigned value in $(this).data(value) when using the jQuery click() method

In my Rails 5 application, I am working on creating a dynamic menu that will guide users to different parts of the site based on their location. The idea is that when they click on a specific menu item from the home page, a modal will appear allowing them ...

Mozilla's client-session data is not persisting

Despite my efforts, the sessions are not persisting between calls. Although similar questions have been asked before, the solutions provided do not address my specific issue. Here is a snippet of the server code: var express = require('express' ...

When refreshing the webpage, it becomes glitchy but then corrects itself

Currently working on a website, I've encountered a frustrating problem that has me scratching my head. This issue occurs across multiple pages; upon refreshing the page, the navbar, search bar, and other elements become completely disorganized. Stra ...

Discover the syntax for reading route parameters in Angular

Currently, I am integrating the Paypal API into my project. After confirming a purchase, Paypal redirects to a specified URL. I set the desired URL as "localhost:4200/shop/order". However, when Paypal returns the URL, it appends the token and payerid at th ...