This is proberbly just an easy problem regarding something i'm overlooking, the code below retrieves data from the cache, so when I enter 'g' for example i get 3 results, or 'f' i get 1 results.
Problem is on the page with the AutoComplete, I get the dropdown below the textbox with 3 lines about 5px high when i enter 'g' or 1 when i enter 'f', but no text inside, or text that small i cannot see it to click.
But if i use the hardcoded values it work ok.
Can anyone spot what i'm missing.
Thanks
George
public ActionResult QuickSearch(string term)
{
var wq = LocationList(term).Select(a =>
new { Value = a.strLocationName });
return Json(wq,JsonRequestBehavior.AllowGet);
}
private List<DisplayTopOneThousandAutoCompleteWeatherLocations> LocationList(string searchString)
{
var wq = _IGTOTACWL.DisplayTopWeatherLocations()
.Where(a => a.strLocationName.StartsWith(searchString, StringComparison.OrdinalIgnoreCase)).ToList();
return wq.ToList();
}
please read the autocomplete docs. by default the source should be an array of strings, or array of {value: value, label: label}. your return json does not match either. its an array of {Value: value}.
CareerChange
Member
43 Points
180 Posts
AutoComplete not showing data in list
Dec 31, 2012 09:17 AM|LINK
Hi
This is proberbly just an easy problem regarding something i'm overlooking, the code below retrieves data from the cache, so when I enter 'g' for example i get 3 results, or 'f' i get 1 results.
Problem is on the page with the AutoComplete, I get the dropdown below the textbox with 3 lines about 5px high when i enter 'g' or 1 when i enter 'f', but no text inside, or text that small i cannot see it to click.
But if i use the hardcoded values it work ok.
Can anyone spot what i'm missing.
Thanks
George
public ActionResult QuickSearch(string term) { var wq = LocationList(term).Select(a => new { Value = a.strLocationName }); return Json(wq,JsonRequestBehavior.AllowGet); } private List<DisplayTopOneThousandAutoCompleteWeatherLocations> LocationList(string searchString) { var wq = _IGTOTACWL.DisplayTopWeatherLocations() .Where(a => a.strLocationName.StartsWith(searchString, StringComparison.OrdinalIgnoreCase)).ToList(); return wq.ToList(); }@using (Html.BeginForm("Index", "SearchResults", FormMethod.Get, new { @id = "frmWeather" })) { <div id="dvWL"> <ul id="ulWeatherSearch"> <li> <input id="wns" name="q" title="Change location" type="text"/></li> <li> <input id="wsb" name="b" type="submit" title="Click to search" value="Weather" /></li> </ul> </div> <input id="hsho" name="o" type="hidden" value="5" /> } <p class="pIP">Location based on IP Address<br />@ViewBag.IPAddr</p> <script> $("#wns").autocomplete({ //source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ], source: '@Url.Action("QuickSearch","Weather")' }); </script>dataType: 'json', parse: function (data) { var rows = new Array(); for (var i = 0; i < data.length; i++) { rows[i] = { data: data[i].strLocationName, value: data[i].strLocationName }; } return rows; }, formatItem: function (row, i, n) { return row.strLocationName; }sp00k
Participant
1916 Points
435 Posts
Re: AutoComplete not showing data in list
Dec 31, 2012 11:20 AM|LINK
return as json array not as your actual list of an object... you need an array of strings.....
CareerChange
Member
43 Points
180 Posts
Re: AutoComplete not showing data in list
Dec 31, 2012 12:28 PM|LINK
Hi spook
Problem seems to be with my javascript, controller is returning data as follows: value=Gosforth, but cannot display data in autocomplete
Thanks
bruce (sqlwo...
All-Star
36852 Points
5446 Posts
Re: AutoComplete not showing data in list
Dec 31, 2012 03:21 PM|LINK
please read the autocomplete docs. by default the source should be an array of strings, or array of {value: value, label: label}. your return json does not match either. its an array of {Value: value}.
CareerChange
Member
43 Points
180 Posts
Re: AutoComplete not showing data in list
Dec 31, 2012 04:00 PM|LINK
Hi Bruce
I'm still having no luck with this, my javascript code is below:
<script> $(function () { $('#wns').autocomplete({ source: '@Url.Action("WeatherQuickSearch","Weather")', dataType: 'json', parse: function (data) { var rows = new Array(); for (var i = 0; i < data.length; i++) { rows[i] = { data: data[i], value: data[i].Location, result: data[i].Location }; } return rows; }, formatItem: function (row, i, n) { return row.Location; } }); }); </script>Controller code:
public JsonResult QuickSearch(string term) { var wq = LocationList(term).Select(a => new { Location = a.strLocationName }); return Json(wq,JsonRequestBehavior.AllowGet); }All i'm getting is the dropdown below the textbox but its empty. I just cannot work out where I'm going wrong
Thanks
George
Gaspard
Contributor
2066 Points
416 Posts
Re: AutoComplete not showing data in list
Dec 31, 2012 07:37 PM|LINK
This tutorial may help
http://theycallmemrjames.blogspot.co.uk/2010/03/jquery-autocomplete-with-aspnet-mvc.html
Regards
CareerChange
Member
43 Points
180 Posts
Re: AutoComplete not showing data in list
Dec 31, 2012 09:24 PM|LINK
Hi Gaspard
Thanks, that link help me fix the problem, one thing i'm confused about, out off all the books i'm reading, none have shown that approach.
I have seem it mentioned on a few posts on stackoverflow, so there must be more than one way to return the data in json format.
Thanks
George