Example of Remote Data in ASP.NET MVC ListView Control
This sample demonstrates the remote data functionalities of the ListView. Click any item from the list to select and highlight it.
- Loading...
@Html.UiListView(new UiListViewOptions { Id = "remotelist", ShowHeader = true, HeaderTitle = "Employees", Height = "340px", DataUrl = Url.Action("ListPeople", "LayoutDemo") })
<style>
.control-section { max-width: 700px; margin: 0 auto; padding: 10px 20px; }
.control-section h4 { margin: 14px 0 6px; }
#flat-list, #group-list { display: inline-block; vertical-align: top; width: 48%; min-width: 260px; margin-right: 1%; }
.lv-row { display: grid; grid-template-columns: 44px 1fr; column-gap: 12px; align-items: center; width: 100%; box-sizing: border-box; padding: 6px 4px; }
.lv-contact { grid-template-columns: 44px 1fr; }
.lv-avatar { display: inline-flex; width: 36px; height: 36px; align-items: center; justify-content: center; border-radius: 50%; background: var(--ui-row-hover-bg); font-size: 14px; font-weight: 600; flex: none; }
</style>
@Html.AntiForgeryToken()
<div class="control-section">
@Html.UiListView(new UiListViewOptions { Id = "remotelist", ShowHeader = true, HeaderTitle = "Employees", Height = "340px", DataUrl = Url.Action("ListPeople", "LayoutDemo") })
</div>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ListViewController : Controller
{
// The page is Views/ListView/RemoteData.cshtml; it draws what ViewData holds.
public ActionResult RemoteData()
{
return View();
}
private static readonly string[] PeopleNames =
{
"Ada Lovelace", "Alan Turing", "Grace Hopper", "Katherine Johnson", "John von Neumann", "Claude Shannon", "Edsger Dijkstra",
"Donald Knuth", "Barbara Liskov", "Frances Allen", "Tim Berners-Lee", "Dennis Ritchie", "Ken Thompson", "Margaret Hamilton",
"Niklaus Wirth", "John McCarthy", "Marvin Minsky", "Radia Perlman", "Vint Cerf", "Bob Kahn", "Leslie Lamport", "Tony Hoare",
"Shafi Goldwasser", "Jean Sammet", "Charles Babbage", "Hedy Lamarr", "Dorothy Vaughan", "Mary Jackson", "Annie Easley",
"Evelyn Berezin", "Karen Sparck Jones", "Adele Goldberg", "Alan Kay", "Ivan Sutherland", "Douglas Engelbart", "Seymour Cray",
"Gordon Moore", "Robert Noyce", "Sophie Wilson", "Steve Furber", "Bjarne Stroustrup", "James Gosling", "Guido van Rossum",
"Anders Hejlsberg", "Brian Kernighan", "Fran Bilas", "Kathleen Booth", "Betty Holberton", "Kay McNulty", "Marlyn Meltzer",
"Ruth Teitelbaum", "Jean Bartik", "Lynn Conway", "Carver Mead", "Butler Lampson", "Chuck Thacker", "Whitfield Diffie",
"Martin Hellman", "Ralph Merkle", "Ron Rivest"
};
private static readonly List<UiListItem> People = PeopleNames.Select((name, i) => new UiListItem
{
Value = (i + 1).ToString(CultureInfo.InvariantCulture),
Text = name,
GroupBy = new[] { "Research", "Engineering", "Operations" }[i % 3]
}).ToList();
/// <summary>DataUrl of the remote ListView samples: a UiDataRequest (skip, take), answered by UiDataOperations.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ListPeople(UiDataRequest request)
{
return Json(UiDataOperations.Execute(People.AsQueryable(), request, "Text"));
}
}
}
@* 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>
@* Layouts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The ListView supports data binding and the DataSource property can be assigned with the instance of DataManager to bind remote the data.
The DataManager that acts as an interface between the service endpoint and ListView will require the following minimal information to interact with the service endpoint properly.
DataManager->url - Defines the service endpoint to fetch the data.
DataManager->adaptor - Defines the adaptor option. By default, the ODataAdaptor is used for remote binding.
Adaptor is responsible for processing response and request from/to the service endpoint. @syncfusion/ej2-data namespace provides some predefined adaptors that are designed to interact with the particular service endpoints. They are:
- UrlAdaptor - Used to interact with any remote services. This is the base adaptor for all remote based adaptors.
- ODataAdaptor - Used to interact with OData endpoints.
- ODataV4Adaptor - Used to interact with OData V4 endpoints.
- WebApiAdaptor - Used to interact with Web API created under OData standards.
- WebMethodAdaptor - Used to interact with web methods.
In this sample, the remote data is bound to be a collection of Products data as an instance of DataManager.