Example of Object Value Binding Functionalities in ASP.NET MVC DropDownList Control
This sample demonstrates the object value binding functionalities of the DropDownList. Click the DropDownList element and select an item from the options list.The corresponding object value of the selected item is then assigned to the value property.
@Html.UiDropDownList("object", new UiDropDownListOptions
{
Placeholder = "Select a Item",
PopupHeight = "200px",
DataSource = Model.RecordList,
Fields = new UiDropDownFields { Text = "Text", Value = "ID" },
OnSelect = "valueChange",
OnChange = "valueChange"
})
<style>
.control-wrapper { margin: 0 auto; width: 250px; }
.property-section .right-side { padding-left: 10px; }
.property-section .left-side { padding: 5px; width: 25%; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
@model DropdownsDemoController.DropDownListRecord
<div class="control-section">
<div class="control-wrapper">
<div id="default" style="padding-top:75px;">
@Html.UiDropDownList("object", new UiDropDownListOptions
{
Placeholder = "Select a Item",
PopupHeight = "200px",
DataSource = Model.RecordList,
Fields = new UiDropDownFields { Text = "Text", Value = "ID" },
OnSelect = "valueChange",
OnChange = "valueChange"
})
</div>
</div>
</div>
<div class="demo-property-panel property-section" title="Properties">
<textarea id="value" style="width: 100%; height: 60px; background-color: inherit;" readonly>Selected value : </textarea>
</div>
<script>
// Syncfusion (AllowObjectBinding): the value is the whole record, shown as JSON.
function valueChange() {
var item = window.Lumen.get('object').getItem();
document.getElementById('value').value = 'Selected value : ' + JSON.stringify(item ? item.data : null);
}
window.addEventListener('load', function () { setTimeout(valueChange, 0); });
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DropDownListController : Controller
{
// The page is Views/DropDownList/ObjectValueBinding.cshtml; it draws what ViewData holds.
public ActionResult ObjectValueBinding()
{
DropDownListRecord model = new DropDownListRecord();
model.RecordList = new DropDownListRecord().RecordModelList();
return View(model);
}
public class DropDownListRecord
{
public string ID { get; set; }
public string Text { get; set; }
public string Group { get; set; }
public List<DropDownListRecord> RecordList { set; get; }
public List<DropDownListRecord> RecordModelList()
{
return Enumerable.Range(1, 150).Select(i => new DropDownListRecord()
{
ID = i.ToString(),
Text = "Item " + i,
Group = "Group " + (char)('A' + (i - 1) % 4)
}).ToList();
}
}
}
}
@* 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>
The DropDownList component contains a list of predefined values from that the user can choose a single value.
The object value binding sample illustrates the use of DropDownList that allows the end-users to select an item from the options list. Once an item is selected, its associated object value is automatically assigned to the value property. This behavior is enabled by the allowObjectBinding property.