How should I send the Key value of the selected item in AutoComplete Textbox to controller instead of the Text value?
Loading
How should I send the Key value of the selected item in AutoComplete Textbox to controller instead of the Text value?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 16, 2022, 1:14 AM EST
Hello Gopi,
Please use this one,
https://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-autocomplete-textbox-in-Asp-Net-mvc-5/
https://stackoverflow.com/questions/4815330/jquery-ui-autocomplete-with-item-and-id
Pankajkumar PatelRank #181 · 13.4K reputationPosted Dec 16, 2022, 8:48 AM EST
Hi Gopi,
Try following:
Index.cshtml
HomeController.cs
using System.Collections.Generic; using System.Linq; using System.Web.Mvc; namespace WebApplication2.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } [HttpPost] public JsonResult Index(string Prefix) { //Note : you can bind same list from database List ObjList = new List() { new City {Id=1,Name="Latur" }, new City {Id=2,Name="Mumbai" }, new City {Id=3,Name="Pune" }, new City {Id=4,Name="Delhi" }, new City {Id=5,Name="Dehradun" }, new City {Id=6,Name="Noida" }, new City {Id=7,Name="New Delhi" } }; //Searching records from list using LINQ query var Name = (from N in ObjList where N.Name.StartsWith(Prefix) select new { N.Name, N.Id }); return Json(Name, JsonRequestBehavior.AllowGet); } } public class City { public int Id { get; set; } public string Name { get; set; } } }For more details:
Hope this will help you!