Example of Foreign Key Column in ASP.NET Core Data Grid Control
In this sample, Customer Name column is a foreign column. You can perform filtering, sorting or editing in the foreign key column.
@* The grid's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiGrid(new UiGridOptions
{
Id = "ForeignKey",
Data = ViewData["datasource"],
AllowFiltering = true,
AllowSorting = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Menu,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 120, TextAlign = UiTextAlign.Right, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", ForeignKeyField = "CustomerID", ForeignKeyValue = "ContactName", DataSource = ViewData["dataSource2"] as System.Collections.IEnumerable, HeaderText = "Contact Name", Width = 150, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "ShipName", HeaderText = "Ship Name", Width = 150 },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 150, EditType = UiEditType.DropDownEdit }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "ForeignKey",
Data = ViewData["datasource"],
AllowFiltering = true,
AllowSorting = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Menu,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 120, TextAlign = UiTextAlign.Right, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", ForeignKeyField = "CustomerID", ForeignKeyValue = "ContactName", DataSource = ViewData["dataSource2"] as System.Collections.IEnumerable, HeaderText = "Contact Name", Width = 150, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "ShipName", HeaderText = "Ship Name", Width = 150 },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 150, EditType = UiEditType.DropDownEdit }
}
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/ForeignKeyColumn.cshtml; it draws what ViewData holds.
public ActionResult ForeignKeyColumn()
{
var order = OrdersDetails.GetAllRecords();
ViewData["datasource"] = order;
var ForeignData = Customer.GetAllRecords();
ViewData["dataSource2"] = ForeignData;
return View();
}
/// <summary>One order: the rows of Syncfusion's Grid samples that use the orders.</summary>
public class OrdersDetails
{
public OrdersDetails()
{
}
public OrdersDetails(int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
{
this.OrderID = OrderID;
this.CustomerID = CustomerId;
this.EmployeeID = EmployeeId;
this.Freight = Freight;
this.ShipCity = ShipCity;
this.Verified = Verified;
this.OrderDate = OrderDate;
this.ShipName = ShipName;
this.ShipCountry = ShipCountry;
this.ShippedDate = ShippedDate;
this.ShipAddress = ShipAddress;
}
public int OrderID { get; set; }
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public double Freight { get; set; }
public string ShipCity { get; set; }
public bool Verified { get; set; }
public DateTime OrderDate { get; set; }
public string ShipName { get; set; }
public string ShipCountry { get; set; }
public DateTime ShippedDate { get; set; }
public string ShipAddress { get; set; }
public static List<OrdersDetails> GetAllRecords()
{
List<OrdersDetails> order = new List<OrdersDetails>();
int code = 10000;
for (int i = 1; i < 10; i++)
{
order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15, 10, 40, 00), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16, 09, 30, 00), "Kirchgasse 6"));
order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04, 09, 30, 00), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11, 03, 45, 00), "Avda. Azteca 123"));
order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30, 03, 45, 00), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7, 07, 50, 00), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22, 07, 30, 00), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30, 06, 12, 00), "Magazinweg 7"));
order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18, 05, 50, 00), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3, 12, 50, 00), "1029 - 12th Ave. S."));
code += 5;
}
return order;
}
}
public class Customer
{
public Customer(){}
public Customer(string CustomerID, string ContactName, string CompanyName, string Address, string Country) {
this.CustomerID = CustomerID;
this.CompanyName = CompanyName;
this.ContactName = ContactName;
this.Address = Address;
this.Country = Country;
}
public static List<Customer> GetAllRecords()
{
List<Customer> cust = new List<Customer>();
cust.Add(new Customer("ALFKI", "Maria", "Alfreds Futterkiste", "Obere Str. 57", "Germany"));
cust.Add(new Customer("ANATR", "Ana Trujillo", "Ana Trujillo Emparedados y helados", "Avda. de la Constitución 2222", "Mexico"));
cust.Add(new Customer("BLONP", "Frédérique Citeaux", "Blondesddsl père et fils", "24, place Kléber", "France"));
cust.Add(new Customer("BOLID", "Martín Sommer", "Bólido Comidas preparadas", "C/ Araquil, 67", "Spain"));
cust.Add(new Customer("ANTON", "Elizabeth Lincoln", "Bottom-Dollar Markets", "23 Tsawassen Blvd.", "Canada"));
//cust.Add(new Customer("BONAP", "Laurence Lebihan", "Bon app", "12, rue des Bouchers", "France"));
//cust.Add(new Customer("BSBEV", "Victoria Ashworth", "B's Beverages", "Fauntleroy Circus", "UK"));
//cust.Add(new Customer("CACTU", "Patricio Simpson", "Cactus Comidas para llevar", "Cerrito 333", "Argentina"));
//cust.Add(new Customer("CENTC", "Francisco Chang", "Centro comercial Moctezuma", "Sierras de Granada 9993", "Mexico"));
//cust.Add(new Customer("CHOPS", "Yang Wang", "Chop-suey Chinese", "Hauptstr. 29", "Switzerland"));
//cust.Add(new Customer("COMMI", "Pedro Afonso", "Comércio Mineiro", "Av. dos Lusíadas, 23", "Brazil"));
//cust.Add(new Customer("CONSH", "Elizabeth Brown", "Consolidated Holdings", "Berkeley Gardens 12 Brewery", "UK"));
//cust.Add(new Customer("DRACD", "Sven Ottlieb", "Drachenblut Delikatessen", "Walserweg 21", "Germany"));
//cust.Add(new Customer("DUMON", "Janine Labrune", "Du monde entier", "67, rue des Cinquante Otages", "France"));
//cust.Add(new Customer("EASTC", "Ann Devon", "Eastern Connection", "35 King George", "UK"));
//cust.Add(new Customer("ERNSH", "Roland Mendel", "Ernst Handel", "Kirchgasse 6", "Austria"));
//cust.Add(new Customer("FAMIA", "Aria Cruz", "Familia Arquibaldo", "Rua Orós, 92", "Brazil"));
//cust.Add(new Customer("FISSA", "Diego Roel", "FISSA Fabrica Inter. Salchichas S.A.", "C/ Moralzarzal, 86", "Spain"));
//cust.Add(new Customer("FOLIG", "Martine Rancé", "Folies gourmandes", "184, chaussée de Tournai", "France"));
//cust.Add(new Customer("FOLKO", "Maria Larsson", "Folk och fä HB", "Åkergatan 24", "Sweden"));
//cust.Add(new Customer("FRANK", "Peter Franken", "Frankenversand", "Berliner Platz 43", "Germany"));
//cust.Add(new Customer("FRANR", "Carine Schmitt", "France restauration", "54, rue Royale", "France"));
//cust.Add(new Customer("FRANS", "Paolo Accorti", "Franchi S.p.A.", "Via Monte Bianco 34", "Italy"));
//cust.Add(new Customer("FURIB", "Lino Rodriguez", "Furia Bacalhau e Frutos do Mar", "Jardim das rosas n. 32", "Portugal"));
//cust.Add(new Customer("GALED", "Eduardo Saavedra", "Galería del gastrónomo", "Rambla de Cataluña, 23", "Spain"));
//cust.Add(new Customer("GODOS", "José Pedro Freyre", "Godos Cocina Típica", "C/ Romero, 33", "Spain"));
//cust.Add(new Customer("GOURL", "André Fonseca", "Gourmet Lanchonetes", "Av. Brasil, 442", "Brazil"));
//cust.Add(new Customer("GREAL", "Howard Snyder", "Great Lakes Food Market", "2732 Baker Blvd.", "USA"));
//cust.Add(new Customer("GROSR", "Manuel Pereira", "GROSELLA-Restaurante", "5ª Ave. Los Palos Grandes", "Venezuela"));
//cust.Add(new Customer("HANAR", "Mario Pontes", "Hanari Carnes", "Rua do Paço, 67", "Brazil"));
//cust.Add(new Customer("HILAA", "Carlos Hernández", "HILARION-Abastos", "Carrera 22 con Ave. Carlos Soublette #8-35", "Venezuela"));
//cust.Add(new Customer("HUNGC", "Yoshi Latimer", "Hungry Coyote Import Store", "City Center Plaza 516 Main St.", "USA"));
//cust.Add(new Customer("HUNGO", "Patricia McKenna", "Hungry Owl All-Night Grocers", "8 Johnstown Road", "Ireland"));
//cust.Add(new Customer("ISLAT", "Helen Bennett", "Island Trading", "Garden House Crowther Way", "UK"));
//cust.Add(new Customer("KOENE", "Philip Cramer", "Königlich Essen", "Maubelstr. 90", "Germany"));
//cust.Add(new Customer("LACOR", "Daniel Tonini", "La corne d'abondance", "67, avenue de l'Europe", "France"));
//cust.Add(new Customer("LAMAI", "Annette Roulet", "La maison d'Asie", "1 rue Alsace-Lorraine", "France"));
//cust.Add(new Customer("LAUGB", "Yoshi Tannamuri", "Laughing Bacchus Wine Cellars", "1900 Oak St.", "Canada"));
//cust.Add(new Customer("LAZYK", "John Steel", "Lazy K Kountry Store", "12 Orchestra Terrace", "USA"));
//cust.Add(new Customer("LEHMS", "Renate Messner", "Lehmanns Marktstand", "Magazinweg 7", "Germany"));
//cust.Add(new Customer("LETSS", "Jaime Yorres", "Let's Stop N Shop", "87 Polk St. Suite 5", "USA"));
//cust.Add(new Customer("LILAS", "Carlos González", "LILA-Supermercado", "Carrera 52 con Ave. Bolívar #65-98 Llano Largo", "Venezuela"));
//cust.Add(new Customer("LINOD", "Felipe Izquierdo", "LINO-Delicateses", "Ave. 5 de Mayo Porlamar", "Venezuela"));
//cust.Add(new Customer("LONEP", "Fran Wilson", "Lonesome Pine Restaurant", "89 Chiaroscuro Rd.", "USA"));
//cust.Add(new Customer("MAGAA", "Giovanni Rovelli", "Magazzini Alimentari Riuniti", "Via Ludovico il Moro 22", "Italy"));
//cust.Add(new Customer("MAISD", "Catherine Dewey", "Maison Dewey", "Rue Joseph-Bens 532", "Belgium"));
//cust.Add(new Customer("MEREP", "Jean Fresnière", "Mère Paillarde", "43 rue St. Laurent", "Canada"));
//cust.Add(new Customer("MORGK", "Alexander Feuer", "Morgenstern Gesundkost", "Heerstr. 22", "Germany"));
//cust.Add(new Customer("NORTS", "Simon Crowther", "North/South", "South House 300 Queensbridge", "UK"));
//cust.Add(new Customer("OCEAN", "Yvonne Moncada", "Océano Atlántico Ltda.", "Ing. Gustavo Moncada 8585 Piso 20-A", "Argentina"));
//cust.Add(new Customer("OLDWO", "Rene Phillips", "Old World Delicatessen", "2743 Bering St.", "USA"));
//cust.Add(new Customer("OTTIK", "Henriette Pfalzheim", "Ottilies Käseladen", "Mehrheimerstr. 369", "Germany"));
//cust.Add(new Customer("PARIS", "Marie Bertrand", "Paris spécialités", "265, boulevard Charonne", "France"));
//cust.Add(new Customer("PERIC", "Guillermo Fernández", "Pericles Comidas clásicas", "Calle Dr. Jorge Cash 321", "Mexico"));
//cust.Add(new Customer("PICCO", "Georg Pipps", "Piccolo und mehr", "Geislweg 14", "Austria"));
//cust.Add(new Customer("PRINI", "Isabel de Castro", "Princesa Isabel Vinhos", "Estrada da saúde n. 58", "Portugal"));
//cust.Add(new Customer("QUEDE", "Bernardo Batista", "Que Delícia", "Rua da Panificadora, 12", "Brazil"));
//cust.Add(new Customer("QUEEN", "Lúcia Carvalho", "Queen Cozinha", "Alameda dos Canàrios, 891", "Brazil"));
//cust.Add(new Customer("QUICK", "Horst Kloss", "QUICK-Stop", "Taucherstraße 10", "Germany"));
//cust.Add(new Customer("RANCH", "Sergio Gutiérrez", "Rancho grande", "Av. del Libertador 900", "Argentina"));
//cust.Add(new Customer("RATTC", "Paula Wilson", "Rattlesnake Canyon Grocery", "2817 Milton Dr.", "USA"));
//cust.Add(new Customer("REGGC", "Maurizio Moroni", "Reggiani Caseifici", "Strada Provinciale 124", "Italy"));
//cust.Add(new Customer("RICAR", "Janete Limeira", "Ricardo Adocicados", "Av. Copacabana, 267", "Brazil"));
//cust.Add(new Customer("RICSU", "Michael Holz", "Richter Supermarkt", "Grenzacherweg 237", "Switzerland"));
//cust.Add(new Customer("ROMEY", "Alejandra Camino", "Romero y tomillo", "Gran Vía, 1", "Spain"));
//cust.Add(new Customer("SANTG", "Jonas Bergulfsen", "Santé Gourmet", "Erling Skakkes gate 78", "Norway"));
//cust.Add(new Customer("SAVEA", "Jose Pavarotti", "Save-a-lot Markets", "187 Suffolk Ln.", "USA"));
//cust.Add(new Customer("SEVES", "Hari Kumar", "Seven Seas Imports", "90 Wadhurst Rd.", "UK"));
//cust.Add(new Customer("SIMOB", "Jytte Petersen", "Simons bistro", "Vinbæltet 34", "Denmark"));
//cust.Add(new Customer("SPECD", "Dominique Perrier", "Spécialités du monde", "25, rue Lauriston", "France"));
//cust.Add(new Customer("SPLIR", "Art Braunschweiger", "Split Rail Beer & Ale", "P.O. Box 555", "USA"));
//cust.Add(new Customer("SUPRD", "Pascale Cartrain", "Suprêmes délices", "Boulevard Tirou, 255", "Belgium"));
//cust.Add(new Customer("THEBI", "Liz Nixon", "The Big Cheese", "89 Jefferson Way Suite 2", "USA"));
//cust.Add(new Customer("THECR", "Liu Wong", "The Cracker Box", "55 Grizzly Peak Rd.", "USA"));
//cust.Add(new Customer("TOMSP", "Karin Josephs", "Toms Spezialitäten", "Luisenstr. 48", "Germany"));
//cust.Add(new Customer("TORTU", "Miguel Angel Paolino", "Tortuga Restaurante", "Avda. Azteca 123", "Mexico"));
//cust.Add(new Customer("TRADH", "Anabela Domingues", "Tradição Hipermercados", "Av. Inês de Castro, 414", "Brazil"));
//cust.Add(new Customer("TRAIH", "Helvetius Nagy", "Trail's Head Gourmet Provisioners", "722 DaVinci Blvd.", "USA"));
//cust.Add(new Customer("VAFFE", "Palle Ibsen", "Vaffeljernet", "Smagsloget 45", "Denmark"));
//cust.Add(new Customer("VICTE", "Mary Saveley", "Victuailles en stock", "2, rue du Commerce", "France"));
//cust.Add(new Customer("VINET", "Paul Henriot", "Vins et alcools Chevalier", "59 rue de l'Abbaye", "France"));
//cust.Add(new Customer("WANDK", "Rita Müller", "Die Wandernde Kuh", "Adenauerallee 900", "Germany"));
//cust.Add(new Customer("WARTH", "Pirkko Koskitalo", "Wartian Herkku", "Torikatu 38", "Finland"));
//cust.Add(new Customer("WELLI", "Paula Parente", "Wellington Importadora", "Rua do Mercado, 12", "Brazil"));
//cust.Add(new Customer("WHITC", "Karl Jablonski", "White Clover Markets", "305 - 14th Ave. S. Suite 3B", "USA"));
//cust.Add(new Customer("WILMK", "Matti Karttunen", "Wilman Kala", "Keskuskatu 45", "Finland"));
//cust.Add(new Customer("WOLZA", "Zbyszek Piestrzeniewicz", "Wolski Zajazd", "ul. Filtrowa 68", "Poland"));
return cust;
}
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string CompanyName { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Grid has option to show foreign key columns. It can be enabled by setting Column.DataSource property with either local or remote data and column field and text can be defined by using Column.ForeignKeyField and Column.ForeignKeyValue properties.