Autocomplete plugin is not working on ASP.NET MVC
Hi,
I am trying to create a web application using ASP.NET MVC and I want add auto complete functionality to one of my text box. So I have added below C# code into my HomeController class to return the data as JSON objects.
- public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult FindPostalAddressesForNumber(string q, int limit)
{
return Json(new City[] { new City {Id = 1, Name = "Colombo"}, new City {Id=2, Name = "Oslo"} });
}
}
Then I have added following JavaScript into my view.
- <script type="text/javascript">
$(document).ready(function () {
$("#postalcode").autocomplete('<%=Url.Action("FindPostalAddressesForNumber", "Home") %>', {
dataType: 'json',
contentType: 'application/json; charset=utf-8',
parse: function (data) {
var rows = new Array();
for (var i = 0; i < data.length; i++) {
rows[i] = { data: data[i], value: data[i].Name, result: data[i].Id };
}
return rows;
},
formatItem: function (row, i, n) {
return row.Name;
}
});
});
</script>
When I run the application the information is not displaying as atocomplete. But when I return data as a string (instead of JSON) the code is working find. Does any one have an idea why the above code is not working?
Rasika.