Example of Default Functionalities in ASP.NET MVC DropDownList Control
This sample demonstrates the default functionalities of the DropDownList in ASP.NET MVC application. Click the DropDownList element and select an item from the options list. The selected item's value and text property values will be shown the in property panel.
| Value | : |
| Text | : |
@Html.UiDropDownList("games", new UiDropDownListOptions
{
Placeholder = "Select a game",
PopupHeight = "220px",
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.UiDropDownList("games", new UiDropDownListOptions
{
Placeholder = "Select a game",
PopupHeight = "220px",
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() || '';
document.getElementById('text').innerHTML = listObj.getText() || '';
}
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/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 DropDownList component contains a list of predefined values from that the user can choose a single value.
The default sample illustrates the use of DropDownList that allows the end-users to select an item from the options list. The selected item's value and text property values will be displayed in the property panel.