In your view
@model Mvcsibaramhyjiya.Models.nameemail
@{
ViewBag.Title = "Autocomp";
}
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" temp_href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js" temp_src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" temp_src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function () {
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#email").bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB &&$(this).data("autocomplete").menu.active) {
event.preventDefault();
}
})
$("#email").autocomplete({
source: function (request, response) {
$.ajax({
url: '/EntryPoint/GetEmailIds', type: "GET", dataType: "json",
data: { query: request.term },
term: extractLast(request.term),
success: function (data) {
response($.map(data, function (item) {
return { label: item, value: item };
}))
}
})
},
search: function () {
var term = extractLast(this.value);
if (term.length < 1) {
return false;
}
},
focus: function () {
return false;
},
select: function (event, ui) {
this.value = ui.item.value;
return false;
}
});
});
</script>
@Html.TextBoxFor(m => m.email)
In your control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvcsibaramhyjiya.Models;
namespace Mvcsibaramhyjiya.Controllers
{
public class EntryPointController : Controller
{
public ActionResult Autocomp()
{
return View();
}
public ActionResult GetEmailIds(string query)
{
query = query.Replace(" ", "");
if (query.Length > 1)
{
int op = query.LastIndexOf(",");
query = query.Substring(op + 1);
}
List<nameemail> obj = new List<nameemail>();
obj.Add(new nameemail { name = "Ved", email = "Ved@Ved" });
obj.Add(new nameemail { name = "veda", email = "veda@veda" });
obj.Add(new nameemail { name = "vedant", email = "vedant@vedant" });
var users = (from u in obj
where u.name.Contains(query)
select u.email).Distinct().ToArray();
return Json(users, JsonRequestBehavior.AllowGet);
}
}
} </nameemail></nameemail>
In your model class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Mvcsibaramhyjiya.Models
{
public class nameemail
{
public string name { get; set; }
public string email { get; set; }
}
}