Example of Search in ASP.NET Core Data Grid Control
This sample demonstrates the Grid searching feature. In this sample, use the search box from toolbar to search Grid records.
@* 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 = "Search",
Data = ViewData["dataSource"],
PageCount = 2,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
Toolbar = new List<string> { "Search" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "CategoryName", HeaderText = "Category Name", Width = 160 },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 170 },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 170, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "QuantityPerUnit", HeaderText = "Quantity Per Unit", Width = 170, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Discontinued", HeaderText = "Discontinued", Type = UiColumnType.Boolean, TextAlign = UiTextAlign.Center, Width = 140, DisplayAsCheckBox = true }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "Search",
Data = ViewData["dataSource"],
PageCount = 2,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
Toolbar = new List<string> { "Search" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "CategoryName", HeaderText = "Category Name", Width = 160 },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 170 },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 170, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "QuantityPerUnit", HeaderText = "Quantity Per Unit", Width = 170, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Discontinued", HeaderText = "Discontinued", Type = UiColumnType.Boolean, TextAlign = UiTextAlign.Center, Width = 140, DisplayAsCheckBox = true }
}
})
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/Search.cshtml; it draws what ViewData holds.
public ActionResult Search()
{
var product = Categories.GetAllRecords().ToList();
ViewData["datasource"] = product;
return View();
}
public class Categories
{
private bool? _discont = false;
public Categories()
{
}
public Categories(string CategoryName, string ProductName, string QuantityPerUnit, bool Discontinued, short UnitsInStock)
{
this.CategoryName = CategoryName;
this.ProductName = ProductName;
this.QuantityPerUnit = QuantityPerUnit;
this.UnitsInStock = UnitsInStock;
this.Discontinued = Discontinued;
}
public static List<Categories> GetAllRecords()
{
List<Categories> category = new List<Categories>();
category.Add(new Categories("Beverages", "Chai", "10 boxes x 20 bags", true, 39));
category.Add(new Categories("Beverages", "Chang", "24 - 12 oz bottles", true, 17));
category.Add(new Categories("Beverages", "Chartreuse verte", "750 cc per bottle", true, 69));
category.Add(new Categories("Beverages", "Cte de Blaye", "12 - 75 cl bottles", false, 17));
category.Add(new Categories("Beverages", "Ipoh Coffee", "16 - 500 g tins", true, 17));
category.Add(new Categories("Beverages", "Lakkalikri", "500 ml", true, 57));
category.Add(new Categories("Beverages", "Laughing Lumberjack Lager", "24 - 12 oz bottles", false, 52));
category.Add(new Categories("Beverages", "Outback Lager", "24 - 355 ml bottles", false, 15));
category.Add(new Categories("Beverages", "Rhnbru Klosterbier", "24 - 0.5 l bottles", false, 125));
category.Add(new Categories("Beverages", "Sasquatch Ale", "24 - 12 oz bottles", true, 111));
category.Add(new Categories("Beverages", "Steeleye Stout", "24 - 12 oz bottles", true, 20));
category.Add(new Categories("Condiments", "Aniseed Syrup", "12 - 550 ml bottles", true, 13));
category.Add(new Categories("Condiments", "Chef Anton's Cajun Seasoning", "48 - 6 oz jars", false, 53));
category.Add(new Categories("Condiments", "Genen Shouyu", "24 - 250 ml bottles", false, 39));
category.Add(new Categories("Condiments", "Grandma's Boysenberry Spread", "12 - 8 oz jars", false, 120));
category.Add(new Categories("Condiments", "Gula Malacca", "20 - 2 kg bags", true, 27));
category.Add(new Categories("Condiments", "Louisiana Fiery Hot Pepper Sauce", "32 - 8 oz bottles", false, 76));
category.Add(new Categories("Condiments", "Louisiana Hot Spiced Okra", "24 - 8 oz jars", false, 4));
category.Add(new Categories("Condiments", "Northwoods Cranberry Sauce", "12 - 12 oz jars", true, 6));
category.Add(new Categories("Condiments", "Original Frankfurter grne Soe", "12 boxes", false, 32));
category.Add(new Categories("Condiments", "Sirop d'rable", "24 - 500 ml bottles", false, 113));
category.Add(new Categories("Condiments", "Vegie-spread", "15 - 625 g jars", false, 24));
category.Add(new Categories("Confections", "Chocolade", "10 pkgs.", true, 15));
category.Add(new Categories("Confections", "Gumbr Gummibrchen", "100 - 250 g bags", false, 15));
category.Add(new Categories("Confections", "Maxilaku", "24 - 50 g pkgs.", false, 10));
category.Add(new Categories("Confections", "NuNuCa Nu-Nougat-Creme", "20 - 450 g glasses", true, 76));
category.Add(new Categories("Confections", "Pavlova", "32 - 500 g boxes", true, 29));
category.Add(new Categories("Confections", "Schoggi Schokolade", "100 - 100 g pieces", true, 49));
category.Add(new Categories("Confections", "Scottish Longbreads", "10 boxes x 8 pieces", true, 6));
category.Add(new Categories("Confections", "Sir Rodney's Marmalade", "30 gift boxes", true, 40));
category.Add(new Categories("Confections", "Sir Rodney's Scones", "24 pkgs. x 4 pieces", true, 3));
category.Add(new Categories("Confections", "Tarte au sucre", "48 pies", false, 17));
category.Add(new Categories("Confections", "Teatime Chocolate Biscuits", "10 boxes x 12 pieces", false, 25));
category.Add(new Categories("Confections", "Valkoinen suklaa", "12 - 100 g bars", false, 65));
category.Add(new Categories("Confections", "Zaanse koeken", "10 - 4 oz boxes", true, 36));
category.Add(new Categories("Dairy Categorys", "Camembert Pierrot", "15 - 300 g rounds", false, 19));
category.Add(new Categories("Dairy Categorys", "Flotemysost", "10 - 500 g pkgs.", false, 26));
category.Add(new Categories("Dairy Categorys", "Geitost", "500 g", false, 112));
category.Add(new Categories("Dairy Categorys", "Gorgonzola Telino", "12 - 100 g pkgs", true, 0));
category.Add(new Categories("Dairy Categorys", "Gudbrandsdalsost", "10 kg pkg.", true, 26));
category.Add(new Categories("Dairy Categorys", "Mascarpone Fabioli", "24 - 200 g pkgs.", true, 9));
category.Add(new Categories("Dairy Categorys", "Mozzarella di Giovanni", "24 - 200 g pkgs.", true, 14));
category.Add(new Categories("Dairy Categorys", "Queso Cabrales", "1 kg pkg.", true, 22));
category.Add(new Categories("Dairy Categorys", "Queso Manchego La Pastora", "10 - 500 g pkgs.", true, 86));
category.Add(new Categories("Dairy Categorys", "Raclette Courdavault", "5 kg pkg.", false, 79));
category.Add(new Categories("Grains/Cereals", "Filo Mix", "16 - 2 kg boxes", false, 38));
category.Add(new Categories("Grains/Cereals", "Gnocchi di nonna Alice", "24 - 250 g pkgs.", false, 21));
category.Add(new Categories("Grains/Cereals", "Gustaf's Knckebrd", "24 - 500 g pkgs.", true, 104));
category.Add(new Categories("Grains/Cereals", "Ravioli Angelo", "24 - 250 g pkgs.", true, 36));
category.Add(new Categories("Grains/Cereals", "Tunnbrd", "12 - 250 g pkgs.", true, 61));
category.Add(new Categories("Grains/Cereals", "Wimmers gute Semmelkndel", "20 bags x 4 pieces", true, 22));
category.Add(new Categories("Meat/Poultry", "Pt chinois", "24 boxes x 2 pies", false, 115));
category.Add(new Categories("Meat/Poultry", "Tourtire", "16 pies", false, 21));
category.Add(new Categories("Produce", "Longlife Tofu", "5 kg pkg.", false, 4));
category.Add(new Categories("Produce", "Manjimup Dried Apples", "50 - 300 g pkgs.", false, 20));
category.Add(new Categories("Produce", "Tofu", "40 - 100 g pkgs.", true, 35));
category.Add(new Categories("Produce", "Uncle Bob's Organic Dried Pears", "12 - 1 lb pkgs.", true, 15));
category.Add(new Categories("Seafood", "Boston Crab Meat", "24 - 4 oz tins", true, 123));
category.Add(new Categories("Seafood", "Carnarvon Tigers", "16 kg pkg.", true, 42));
category.Add(new Categories("Seafood", "Escargots de Bourgogne", "24 pieces", true, 62));
category.Add(new Categories("Seafood", "Gravad lax", "12 - 500 g pkgs.", true, 11));
category.Add(new Categories("Seafood", "Ikura", "12 - 200 ml jars", false, 31));
category.Add(new Categories("Seafood", "Inlagd Sill", "24 - 250 g jars", false, 112));
category.Add(new Categories("Seafood", "Jack's New England Clam Chowder", "12 - 12 oz cans", false, 85));
category.Add(new Categories("Seafood", "Konbu", "2 kg box", false, 24));
category.Add(new Categories("Seafood", "Nord-Ost Matjeshering", "10 - 200 g glasses", false, 10));
category.Add(new Categories("Seafood", "Rd Kaviar", "24 - 150 g jars", false, 101));
category.Add(new Categories("Seafood", "Rogede sild", "1k pkg.", false, 5));
category.Add(new Categories("Seafood", "Spegesild", "4 - 450 g glasses", true, 95));
return category;
}
public string CategoryName { get; set; }
public string ProductName { get; set; }
public string QuantityPerUnit { get; set; }
public short UnitsInStock { get; set; }
public bool? Discontinued {
get
{
return this._discont;
}
set
{
this._discont = value;
}
}
}
}
}
@* 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. *@
The Grid has an option to search its content using the search method with search key as the parameter.
In this demo, The Grid toolbar provides an option to search the Grid's records. The user can type the text box in the toolbar and click search button or press Enter key to perform search operation. We have used search method to initiate search.