Click here to Skip to main content
Click here to Skip to main content

Tagged as

Autocomplete in ASP.NET MVC3

, 12 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Autocomplete behaviour in a strongly typed view of ASP.NET MVC3
Provide a link:
<link href="../../Content/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
 
write a script as:
$(function () {
              function split(val) {
                  return val.split(/,\s*/);
              }
              function extractLast(term) {
                  return split(term).pop();
              }
 
              $("#NewAddress").bind("keydown", function (event) {
                  if (event.keyCode === $.ui.keyCode.TAB &&
                      $(this).data("autocomplete").menu.active) {
                      event.preventDefault();
                  }
              })
              $("#NewAddress").autocomplete({
                  source: function (request, response) {
                      //define a function to call your Action (assuming UserController)
                      $.ajax({
                          url: '/Controller/action', type: "GET", dataType: "json",
                          //query will be the param used by your action method
                          data: { query: request.term },
                          term: extractLast(request.term),
                          success: function (data) {
                              response($.map(data, function (item) {
                                  return { label: item, value: item };
                              }))
                          }
                      })
                  },
                  search: function () {
                      // custom minLength
                      var term = extractLast(this.value);
                      if (term.length < 1) {
                          return false;
                      }
                  },
                  focus: function () {
                      // prevent value inserted on focus
                      return false;
                  },
                  select: function (event, ui) {
                      var terms = split(this.value);
                      // remove the current input
                      terms.pop();
                      // add the selected item
                      terms.push(ui.item.value);
                      // add placeholder to get the comma-and-space at the end
                      terms.push("");
                      this.value = terms.join(", ");
                      return false;
                  }
              });
 
          });
 

In the view body write:
<%=Html.TextBoxFor(m => m.NewAddress)%>
 
in the controller :
public ActionResult GetEmailIds(string query)
       {
           query = query.Replace(" ", "");
           if (query.Length > 1)
           {
               int op = query.LastIndexOf(",");
               query = query.Substring(op + 1);
           }
           var users = (from u in _userService.GetAllUsers()
                        where u.EmailAddress.Contains(query)
                        orderby u.EmailAddress // optional
                        select u.EmailAddress).Distinct().ToArray();
 
           return Json(users, JsonRequestBehavior.AllowGet);
       }

License

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

Share

About the Author

member60
Software Developer
Other Other
No Biography provided

Comments and Discussions

 
QuestionA huge thank PinmemberMember 1065006927-Mar-14 0:29 
GeneralMy vote of 3 Pinmemberkathir831-Apr-13 3:51 
GeneralMy vote of 5 Pinmembernamrathamahesh12-Feb-13 1:23 
Questiongreate post PinmemberEtibar HASANOV21-Nov-12 3:14 
QuestionFunction not running working PinmemberMember 929708425-Jul-12 0:36 
AnswerRe: Function not running working PinmemberDiliposv22-Feb-13 20:36 
GeneralReason for my vote of 5 really useful for ASP.NET developers Pinmembernikhi _singh1-Mar-12 18:53 
GeneralRe: Reason for my vote of 5really useful for ASP.NET developers Pinmembermember6023-Apr-12 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 12 Mar 2012
Article Copyright 2012 by member60
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid