Example of Object Value Binding Functionalities in ASP.NET MVC ComboBox Control
This sample demonstrates the object value binding functionalities of the ComboBox. Type a character in the ComboBox element or click the drodown icon to choose an item from the options list. The corresponding object value of the selected item is then assigned to the value property. In the property panel, the value property of the selected item's will be displayed.
@Html.UiComboBox("object", new UiComboBoxOptions
{
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.ComboBoxRecord
<div class="control-section">
<div class="control-wrapper">
<div id="default" style="padding-top:75px;">
@Html.UiComboBox("object", new UiComboBoxOptions
{
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 ComboBoxController : Controller
{
// The page is Views/ComboBox/ObjectValueBinding.cshtml; it draws what ViewData holds.
public ActionResult ObjectValueBinding()
{
ComboBoxRecord model = new ComboBoxRecord();
model.RecordList = new ComboBoxRecord().RecordModelList();
return View(model);
}
public class ComboBoxRecord
{
public string ID { get; set; }
public string Text { get; set; }
public string Group { get; set; }
public List<ComboBoxRecord> RecordList { set; get; }
public List<ComboBoxRecord> RecordModelList()
{
return Enumerable.Range(1, 150).Select(i => new ComboBoxRecord()
{
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 ComboBox component allows the user to type a value, or choose an option from the list of predefined options.Once an item is selected, its associated object value is automatically assigned to the value property. This behavior is enabled by the allowObjectBinding property.