Example of Remote Data in ASP.NET MVC DropDownTree Control
This sample explains you about how to bind data to the Dropdown Tree from a remote data source. Click the Dropdown Tree element, and then select an item from the hierarchical structure suggestion list.
@Html.UiDropDownTree("ddtremote", new UiDropDownTreeOptions
{
Placeholder = "Select a name",
PopupHeight = "200px",
DataUrl = Url.Action("Folders", "DropdownsDemo"),
Fields = new UiDropDownFields { Value = "Id", Text = "Name", Child = "Children", Expanded = "Open" }
})
<style>
.control-section .content { margin: 0 auto; width: 250px; }
.control-section h4 { margin: 0 0 8px; }
</style>
<div class="control-section">
<div class="content" style="padding-top:75px">
@Html.UiDropDownTree("ddtremote", new UiDropDownTreeOptions
{
Placeholder = "Select a name",
PopupHeight = "200px",
DataUrl = Url.Action("Folders", "DropdownsDemo"),
Fields = new UiDropDownFields { Value = "Id", Text = "Name", Child = "Children", Expanded = "Open" }
})
</div>
</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 DropDownTreeController : Controller
{
// The page is Views/DropDownTree/RemoteData.cshtml; it draws what ViewData holds.
public ActionResult RemoteData()
{
return View();
}
/// <summary>DataUrl of the remote DropDownTree: folders with their children (Fields.Child = "Children"), read once.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Folders()
{
var folders = new object[]
{
new { Id = "1", Name = "Discover Music", Open = true, Children = new object[] { new { Id = "3", Name = "Hot Singles" }, new { Id = "4", Name = "Rising Artists" }, new { Id = "5", Name = "Live Music" } } },
new { Id = "2", Name = "Sales and Events", Open = false, Children = new object[] { new { Id = "6", Name = "Album Sale" }, new { Id = "7", Name = "Live Events" } } },
new { Id = "8", Name = "Categories", Open = true, Children = new object[] { new { Id = "9", Name = "Songs" }, new { Id = "10", Name = "Bestselling Albums" } } }
};
return Json(folders);
}
}
}
@* 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>
@* Dropdowns with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The Dropdown Tree loads the data from the remote data services through the DataSource property. It supports the data type of array or DataManager.
The DataManager that act as an interface between the service endpoint and Dropdown Tree, will require the below minimal information to interact with the service endpoint properly.
DataManager->url- Defines the service endpoint to fetch data-
DataManager->adaptor- Defines the adaptor option. By default,ODataAdaptoris used for remote binding.
Adaptor is responsible for processing response and request from or to the service endpoint. The DataManager provides some pre-defined adaptors that are designed to interact with the particular service endpoints. They are:
UrlAdaptor- To interact with any remote services.ODataAdaptor- To interact with OData endpoints.ODataV4Adaptor- To interact with OData V4 endpoints.WebApiAdaptor- To interact with Web API created under OData standards.WebMethodAdaptor- To interact with web methods.
In this demo, the Dropdown Tree is bound with the dataSource from the Northwind remote service by using the DataManager instance.