Example of DynamicObject Binding in ASP.NET Core Data Grid Control
This sample shows how to use DynamicObject data binding with CRUD operations, sorting and filtering in the Grid component.
@* 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 = "DynamicObjectGrid",
Data = ViewData["DynamicData"],
AllowSorting = true,
AllowFiltering = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.FilterBar,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Normal },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", 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.Left, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "DynamicObjectGrid",
Data = ViewData["DynamicData"],
AllowSorting = true,
AllowFiltering = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.FilterBar,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Normal },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", 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.Left, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
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/DynamicobjectBinding.cshtml; it draws what ViewData holds.
public ActionResult DynamicobjectBinding()
{
string[] customerIDs = { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" };
string[] shipCountrys = { "USA", "UK", "Denmark", "Australia", "India" };
var DynamicOrders = Enumerable.Range(1, 75).Select((x) =>
{
dynamic d = new DynamicList();
d.OrderID = 1000 + x;
d.CustomerID = customerIDs[x % customerIDs.Length];
d.Freight = (new double[] { 2, 1, 4, 5, 3 })[x % 5] * x;
d.OrderDate = (new DateTime[] { new DateTime(2010, 11, 5), new DateTime(2018, 10, 3), new DateTime(1995, 9, 9), new DateTime(2012, 8, 2), new DateTime(2015, 4, 11) })[new Random().Next(5)];
d.ShipCountry = shipCountrys[x % shipCountrys.Length];
return d;
}).Cast<DynamicList>().ToList<DynamicList>();
ViewData["DynamicData"] = DynamicOrders;
return View();
}
public class DynamicList : DynamicObject
{
private List<KeyValuePair<string, object>> properties = new List<KeyValuePair<string, object>>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
string name = binder.Name;
var property = properties.Find(p => p.Key == name);
result = property.Value;
return property.Key != null;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
string name = binder.Name;
var property = properties.Find(p => p.Key == name);
if (property.Key != null)
{
properties.Remove(property);
}
properties.Add(new KeyValuePair<string, object>(name, value));
return true;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return properties.ConvertAll(p => p.Key);
}
}
}
}
@* 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 DynamicObject data binding. The DataSource property can be assigned as list of DynamicObject.