Click here to Skip to main content
16,019,089 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everyone hope all of you are good...
i am new to MVC and i am facing a problem i want to autocomplete the textbox like google chrome with my hardcoded data using jquery but do not get any success, i have added the reference of jquery autocomplete API but nothing happened kindly help me to overcome this issue .. thankx in advance

What I have tried:

Model:
public class Locations
    {
 
        public int Id { get; set; }
        public string Name { get; set; }
 
        public List<Locations> locations = new List<Locations>()
        {
            new Locations() {Id = 1, Name = "London"},
            new Locations() {Id = 2, Name = "Walles"},
            new Locations() {Id = 3, Name = "Birmingham"},
            new Locations() {Id = 4, Name = "Edinburgh"},
            new Locations() {Id = 5, Name = "Glasgow"},
            new Locations() {Id = 6, Name = "Liverpool"},
            new Locations() {Id = 7, Name = "Bristol"},
            new Locations() {Id = 8, Name = "Manchester"},
            new Locations() {Id = 9, Name = "NewCastle"},
            new Locations() {Id = 10, Name = "Leeds"},
            new Locations() {Id = 11, Name = "Sheffield"},
            new Locations() {Id = 12, Name = "Nottingham"},
            new Locations() {Id = 13, Name = "Cardif"},
            new Locations() {Id = 14, Name = "Cambridge"},
            new Locations() {Id = 15, Name = "Bradford"},
            new Locations() {Id = 16, Name = "Kingston Upon Hall"},
            new Locations() {Id = 17, Name = "Norwich"},
            new Locations() {Id = 18, Name = "Conventory"}
        };
    }


Controller:

public class DefaultController : Controller
   {
       public ActionResult Index()
       {
           return View();
       }

       public JsonResult Search(string term)
       {
           Locations l = new Locations();
           List<string> Loc;
           Loc = l.locations.Where(x => x.Name.StartsWith(term)).Select(x => x.Name).Distinct().ToList();
           return Json(Loc, JsonRequestBehavior.AllowGet);
       }
   }


View:
@model IEnumerable<SearchBox.Models.Locations>
@using SearchBox.Models
@{
    ViewBag.Title = "Index";
}
<link href="~/Content/jquery-ui.css" rel="stylesheet" />
<script src="~/Content/jquery-ui.js"></script>
    <script type="text/javascript">
    $(function () {
        $('#searchText').autocomplete({
            source: '@Url.Action("Search")' 
            });
    })
</script>
 

 
<h2>Search Demo</h2>
@using (Html.BeginForm())
{
   <input type = "text" id="searchText" name="searchText" />
   <input type="submit" value="Search" />
}
Posted
Updated 20-Apr-17 2:14am
Comments
CHill60 20-Apr-17 8:11am    
What is the problem with the answer on your earlier post? Autocomplete textbox in ASP.NET MVC[^]
Muhammd Aamir 20-Apr-17 8:12am    
@CHill60 i did not get the right answer for that post ....
[no name] 20-Apr-17 8:19am    
So? Doesn't mean the you get to rudely repost the same question over and over.
Muhammd Aamir 20-Apr-17 8:58am    
NotPoliticallyCorrect Got it ... will void this in future
Muhammd Aamir 20-Apr-17 8:13am    
if you have any solution kindly help me

1 solution


you can use this
$('#searchText').autocomplete({
           source: function (query, process) {
                   return $.post("@Url.Content("~/TestController/GetList")", { term: query }, function (data) {
               return process(data);
           });
});
           });
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900