-1

am using autocomplete ajax function on text-box. 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 type="text/javascript">
        $(document).ready(function () {
            $("#txtValue").autocomplete({
                source: '@Url.Action("Getbusiness")',
                minLength: 1
            });
            $("#txtLocation").autocomplete({
                source: '@Url.Action("GetLocation")',
                minLength: 1
            });
        });
    </script>
  • Here you will find the answer of question http://stackoverflow.com/questions/21951115/remove-duplicate-values-from-json-data – Mukaram Aug 22 '15 at 10:36
  • i was so confused.. actually am fresher in I.T .. can u help me to solve this sir ? – Azkar Riyaz Aug 22 '15 at 10:48

2 Answers2

0

there are many options by which you can achieve this either by using jQuery to make the string unique or simply calling a php unique function before passing the result to the ajax request

Mukaram
  • 105
  • 1
  • 3
0

This is a demo that how it will help you first get the result of the your function on and then user array filter function that will return the unique element for your array

var a=// get the content of your ajax request here .

var unique=a.filter(function(itm,i,a){
    return i==a.indexOf(itm);
});

alert(unique);
Mukaram
  • 105
  • 1
  • 3