Example of Dialog Template in ASP.NET Core Data Grid Control
This sample demonstrates CRUD operations in Grid with Dialog Template. You can perform CRUD operations as follows, Add - To add new record, click Add toolbar button Edit - To edit record, double click a row or click toolbar Edit button after selected a row Delete - To delete record, click toolbar Delete button after selected a row Update , Cancel - You can save or discard changes by click toolbar Update and Cancel button respectively
@Html.UiGrid(new UiGridOptions
{
Id = "DialogTemplateEdit",
Data = ViewData["dataSource"],
PageCount = 2,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Dialog, Template = "#dialogtemplate" },
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", HeaderText = "Customer Name", Width = 150, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 150, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit }
}
})
<style>
.form-group.col-md-6 {
width: 250px;
}
#ShipAddress {
resize: vertical;
}
:-ms-fullscreen, .e-dialog {
max-width: 552px;
}
@@media only screen and (max-width: 991px) {
.e-dialog.e-control.e-popup {
min-width: 300px !important;
}
}
@@media only screen and (min-width: 992px) {
.e-edit-dialog.e-dialog.e-control.e-popup {
min-width: 550px !important;
min-height: 415px;
}
.e-bigger.e-edit-dialog.e-dialog.e-control.e-popup,
.e-bigger .e-edit-dialog.e-dialog.e-control.e-popup {
min-height: 496px;
}
}
</style>
<template id="dialogtemplate">
<div id="dialogTemp" class="dialog-form">
<div class="form-row"><label for="dlgOrderID">Order ID</label><input id="dlgOrderID" name="OrderID" type="number" /></div>
<div class="form-row"><label for="dlgCustomerID">Customer Name</label><input id="dlgCustomerID" name="CustomerID" type="text" /></div>
<div class="form-row"><label for="dlgFreight">Freight</label><input id="dlgFreight" name="Freight" type="number" step="0.01" /></div>
<div class="form-row"><label for="dlgOrderDate">Order Date</label><input id="dlgOrderDate" name="OrderDate" type="date" /></div>
<div class="form-row"><label for="dlgShipCountry">Ship Country</label>
<select id="dlgShipCountry" name="ShipCountry"><option>Denmark</option><option>Brazil</option><option>Germany</option><option>Austria</option><option>Switzerland</option></select></div>
</div>
</template>
@Html.UiGrid(new UiGridOptions
{
Id = "DialogTemplateEdit",
Data = ViewData["dataSource"],
PageCount = 2,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Dialog, Template = "#dialogtemplate" },
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", HeaderText = "Customer Name", Width = 150, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 150, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", 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/DialogTemplate.cshtml; it draws what ViewData holds.
public ActionResult DialogTemplate()
{
var order = OrdersDetails.GetAllRecords();
ViewData["datasource"] = order;
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;
}
}
}
}
@* 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 supports CRUD operations. This CRUD operations can be configured in Grid using editSettings.
In this demo, Dialog template is enabled for editing by defining editSettings.mode as Dialog and editSettings.template as SCRIPT element`s ID which holds the template. You can start editing by double clicking a row or clicking on toolbar's Edit button, then the currently selected row will be shown on a dialog with custom elements and you can change the row values and save edited data to the datasource.