Example of Empty Record Template in ASP.NET MVC Data Grid Control
This sample demonstrates the usage of the empty record template in the DataGrid. In this sample, we show a custom image in the place of the default no-record message typically shown by the DataGrid.
There is no data available to display at the moment.
@Html.UiGrid(new UiGridOptions
{
Id = "EmptyRecordTemplate",
Data = new List<object>(),
Height = "300px",
EmptyRecordTemplate = "#emptytemplate",
AllowPaging = true,
PageSize = 12,
PageCount = 5,
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 = 140, TextAlign = UiTextAlign.Right, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 150, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 140, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 150, Format = "M/d/yyyy hh:mm tt", EditType = UiEditType.DateTimePickerEdit },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 150 },
new UiGridColumn { Field = "ShipAddress", HeaderText = "Ship Address", Width = 200 },
new UiGridColumn { Field = "Verified", HeaderText = "Verified", Type = UiColumnType.Boolean, Width = 150, EditType = UiEditType.BooleanEdit, DisplayAsCheckBox = true }
}
})
<style>
.emptyRecordTemplate { text-align: center; }
.emptyRecordTemplate .e-emptyRecord { display: block; margin: 10px auto; }
</style>
<template id="emptytemplate">
<div class="emptyRecordTemplate">
<svg class="e-emptyRecord" width="120" height="90" viewBox="0 0 120 90" role="img" aria-label="No record" xmlns="http://www.w3.org/2000/svg"><rect x="14" y="14" width="92" height="62" rx="6" fill="none" stroke="#9aa5b1" stroke-width="3"/><path d="M14 34h92M44 34v42" stroke="#9aa5b1" stroke-width="3" fill="none"/><circle cx="82" cy="58" r="9" fill="none" stroke="#9aa5b1" stroke-width="3"/></svg>
<div>There is no data available to display at the moment.</div>
</div>
</template>
@Html.UiGrid(new UiGridOptions
{
Id = "EmptyRecordTemplate",
Data = new List<object>(),
Height = "300px",
EmptyRecordTemplate = "#emptytemplate",
AllowPaging = true,
PageSize = 12,
PageCount = 5,
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 = 140, TextAlign = UiTextAlign.Right, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 150, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 140, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 150, Format = "M/d/yyyy hh:mm tt", EditType = UiEditType.DateTimePickerEdit },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 150 },
new UiGridColumn { Field = "ShipAddress", HeaderText = "Ship Address", Width = 200 },
new UiGridColumn { Field = "Verified", HeaderText = "Verified", Type = UiColumnType.Boolean, Width = 150, EditType = UiEditType.BooleanEdit, 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/EmptyRecordTemplate.cshtml; it draws what ViewData holds.
public ActionResult EmptyRecordTemplate()
{
// the grid has no rows: its empty record template is shown
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The DataGrid provides a way to use a custom content when it has no data to present. The emptyRecordTemplate property accepts either a string or an HTML element ID value, which will be used as the template when there’s no data.