Example of Default Functionalities in ASP.NET MVC AutoComplete Control
This sample demonstrates the default functionalities of the AutoComplete in ASP.NET MVC application. Type a character in the AutoComplete element and choose an item from the suggestion list.
@Html.UiAutoComplete("games", new UiAutoCompleteOptions { Items = ((string[])ViewData["data"]).Select(text => new UiListItem(text, text)).ToList(), Placeholder = "e.g. Basketball" })
<style>
.control-wrapper { margin: 0 auto; width: 300px; }
</style>
<div class="control-section">
<div class="control-wrapper">
<div style="padding-top:75px;">
@Html.UiAutoComplete("games", new UiAutoCompleteOptions { Items = ((string[])ViewData["data"]).Select(text => new UiListItem(text, text)).ToList(), Placeholder = "e.g. Basketball" })
</div>
</div>
</div>
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/DefaultFunctionalities.cshtml; it draws what ViewData holds.
public ActionResult DefaultFunctionalities()
{
ViewData["data"] = new string[] { "American Football", "Badminton", "Basketball", "Cricket", "Football", "Golf", "Hockey", "Rugby", "Snooker", "Tennis" };
return View();
}
}
}
@* 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 provides the matched suggestion list when a character is typed in the input, from that the user can select one.
The default sample illustrates the use of AutoComplete that allows the end-users to select an item from the suggestion list.