Ruga Lincy

Ruga Lincy

  • NA
  • 42
  • 6.6k

Avoid displaying repeated data on auto-complete textbox

Aug 22 2015 7:12 AM

am using autocomplete ajax function on text-box MVC4.

till what i have done is. using ajax auto-complete function it will fetch all data from table columns.

Example :

In my columns U.S.A, Canada,London ,u.s.a,India,china,London,u.s.a, etc When i type on textbox as u.s.a. it fetch u.s.a,u.s.a,u.s.a,

Problem is,

i don't need to fetch repeated data when i type u.s.a

it should return u.s.a

not u.s.a,u.s.a,u.s.a,

Controller

 
public JsonResult GetLocation(string term)
{
SYTEntities db = new SYTEntities();
List<string> Location = new List<string>();
{
Location.AddRange(db.tblAddresses.Where(x => x.City.StartsWith(term))
.Select(y => y.City).ToList());
Location.AddRange(db.tblAddresses.Where(x => x.State.StartsWith(term))
.Select(y => y.State).ToList());
}
return Json(Location, JsonRequestBehavior.AllowGet);
}
 
view
<input type="search" name="txtLocation" id="txtLocation" value="@Session["location"]" placeholder=" City or State"> 
 
<script src="~/Scripts/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#txtValue").autocomplete({
source: '@Url.Action("Getbusiness")',
minLength: 1
});
$("#txtLocation").autocomplete({
source: '@Url.Action("GetLocation")',
minLength: 1
});
});
</script>
 

Answers (3)