Example of Default Functionalities in ASP.NET MVC ComboBox Control
This sample demonstrates the default functionalities of the ComboBox in ASP.NET MVC application. Type a character in the ComboBox element or click the drodown icon to choose an item from the options list. The selected item's value and text property values will be shown in the property panel.
| Value | : |
| Text | : |
@Html.UiComboBox("games", new UiComboBoxOptions
{
Placeholder = "Select a game",
PopupHeight = "230px",
Value = "Game3",
DataSource = (IEnumerable<DropdownsDemoController.GameList>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Game", Value = "Id" },
OnChange = "valueChange"
})
<style>
.control-wrapper { margin: 0 auto; width: 300px; }
.property-section .right-side { padding-left: 10px; }
.property-section .left-side { padding: 5px; width: 25%; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div class="control-wrapper">
<div id="default" style="padding-top:75px;">
@Html.UiComboBox("games", new UiComboBoxOptions
{
Placeholder = "Select a game",
PopupHeight = "230px",
Value = "Game3",
DataSource = (IEnumerable<DropdownsDemoController.GameList>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Game", Value = "Id" },
OnChange = "valueChange"
})
</div>
</div>
</div>
<div class="demo-property-panel property-section" title="Properties">
<table id="property" style="width: 100%; margin: 10px">
<tr>
<td class="left-side">Value</td>
<td>:<span id="value" class="right-side"></span></td>
</tr>
<tr>
<td class="left-side">Text</td>
<td>:<span id="text" class="right-side"></span></td>
</tr>
</table>
</div>
<script>
// Syncfusion: the Value and Text properties of the DropDownList are shown in the property panel.
function valueChange() {
var listObj = window.Lumen.get('games');
document.getElementById('value').innerHTML = listObj.getValue() === null ? 'null' : listObj.getValue();
document.getElementById('text').innerHTML = listObj.getText() ? listObj.getText() : '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/DefaultFunctionalities.cshtml; it draws what ViewData holds.
public ActionResult DefaultFunctionalities()
{
ViewData["data"] = new GameList().GameLists();
return View();
}
public class GameList
{
public string Id { get; set; }
public string Game { get; set; }
public List<GameList> GameLists()
{
List<GameList> game = new List<GameList>();
game.Add(new GameList { Id = "Game1", Game = "American Football" });
game.Add(new GameList { Id = "Game2", Game = "Badminton" });
game.Add(new GameList { Id = "Game3", Game = "Basketball" });
game.Add(new GameList { Id = "Game4", Game = "Cricket" });
game.Add(new GameList { Id = "Game5", Game = "Football" });
game.Add(new GameList { Id = "Game6", Game = "Golf" });
game.Add(new GameList { Id = "Game7", Game = "Hockey" });
game.Add(new GameList { Id = "Game8", Game = "Rugby" });
game.Add(new GameList { Id = "Game9", Game = "Snooker" });
game.Add(new GameList { Id = "Game10", Game = "Tennis" });
return game;
}
}
}
}
@* 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.