Example of Object Value Binding Functionalities in ASP.NET MVC AutoComplete Control
The sample showcases the functionality of object value binding in the AutoComplete component. Users can type characters into the AutoComplete field and select an item from the suggestion 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.UiAutoComplete("object", new UiAutoCompleteOptions
{
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.AutoCompleteRecord
<div class="control-section">
<div class="control-wrapper">
<div id="default" style="padding-top:75px;">
@Html.UiAutoComplete("object", new UiAutoCompleteOptions
{
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 AutoCompleteController : Controller
{
// The page is Views/AutoComplete/ObjectValueBinding.cshtml; it draws what ViewData holds.
public ActionResult ObjectValueBinding()
{
AutoCompleteRecord model = new AutoCompleteRecord();
model.RecordList = new AutoCompleteRecord().RecordModelList();
return View(model);
}
public class AutoCompleteRecord
{
public string ID { get; set; }
public string Text { get; set; }
public string Group { get; set; }
public List<AutoCompleteRecord> RecordList { set; get; }
public List<AutoCompleteRecord> RecordModelList()
{
return Enumerable.Range(1, 150).Select(i => new AutoCompleteRecord()
{
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 AutoComplete component generates a list of suggestions that match the characters typed by the user. Users can then select an item from this 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.