0

I have an auto complete method.

$("#txtSearch").autocomplete({

                  source: function (request, response) {
                      $.ajax({
                          url: "/Home/Getsrchresult",
                          type: "POST",
                          dataType: "json",
                          data: { term: request.term, location: $('#location').val() },
                          success: function (data) {
                              response($.map(data, function (item) {
                                  return {


                                   label: item.srchresult, value: item.srchresult
                                    };
                              }))

                          }
                      })
                  }
              });

my controller passes multidimension array.How to place all data inside autocmplete textbox

controller

                var fd2 = (from r in db.Restaurants
                       where r.restname.ToLower().Contains(term.ToLower())
                       orderby r.state == location descending
                       select new { searchresult = r.restname ,place=r.place


                       }).Take(10);
  return Json(fd2, JsonRequestBehavior.AllowGet);

Response is like this

[{"srchresult":"foodtakeaway","place":"karnataka"},{"srchresult":"ssdf","place":"dfsaf"}]
1
  • It is an array of objects, not multidimension array. Commented Feb 28, 2014 at 15:45

1 Answer 1

1

This should work

$("#txtSearch").autocomplete({
     source: function (request, response) {                  
               $.ajax({
                    url: "/Home/Getsrchresult",
                    type: "POST",
                    dataType: "json",      
                    data: { term: request.term,location:$('#location').val() },
                    success: function (data) {
                        response($.map(data, function (item) {                   
                            return { label: item.place, value: item.place };
                        }))
                    }
                })
            }  
});

Assuming you are returning JSON like this

[
    {
        "srchresult": "foodtakeaway",
        "place": "karnataka"
    },
    {
        "srchresult": "ssdf",
        "place": "dfsaf"
    }
]

I have used item.place for label and value property, you may replace it with item.srchresult as needed.

Suggestion : Use MVC Helper methods(Url.Action) to generate the url to your action methods, instead of hardcoding

Sign up to request clarification or add additional context in comments.

6 Comments

i have 2 items place and srchresult
This will work as long as you return n number of items with the JSON structure you mentioned.
can i retrieve both srchresult and place in txtsearch.simultaneously
but i am getting location from javascript.so i cant send javascript variabled in url.Action
this is not the answer i required.pls understand my situation
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.