Here, we are going to implement Autocomplete TextBox, using jQuery and MVC 5. Simply follow the steps given below.
Step 1
Go to Microsoft Visual Studio 2015 and create a new empty MVC 5 project.
Step 2
Now, go to Controllers folder and add new empty HomeController.cs file. Paste the code given below.
HomeController.cs
- usingJQueryAutoComplete.Models;
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Mvc;
- namespaceJQueryAutoComplete.Controllers {
- publicclassHomeController: Controller {
- publicActionResult Index() {
- returnView();
- }
- [HttpPost]
- publicJsonResult Index(stringkeyword) {
- //This can be replaced with database call.
- List < Games > objGameList = newList < Games > () {
- newGames {
- Id = 1, GameName = "Cricket"
- },
- newGames {
- Id = 2, GameName = "Football"
- },
- newGames {
- Id = 3, GameName = "Chesh"
- },
- newGames {
- Id = 4, GameName = "VallyBall"
- },
- newGames {
- Id = 5, GameName = "Kabbadi"
- },
- newGames {
- Id = 6, GameName = "Hockey"
- }
- };
- var result = (from a inobjGameList where a.GameName.ToLower().StartsWith(keyword.ToLower()) select new {
- a.GameName
- });
- returnJson(result, JsonRequestBehavior.AllowGet);
- }
- publicActionResult About() {
- ViewBag.Message = "Your application description page.";
- returnView();
- }
- publicActionResult Contact() {
- ViewBag.Message = "Your contact page.";
- returnView();
- }
- }
- }
Step 3
Now, add a new class named Games.cs in Models folder. Add the code given below in it.
Games.cs
Step 4
- namespaceJQueryAutoComplete.Models {
- publicclassGames {
- publicintId {
- get;
- set;
- }
- publicstringGameName {
- get;
- set;
- }
- }
- }
Go to BundleConfig.cs and comment the lines given below.







Join the conversation! Your thoughts help the community grow.