I am looking at the book MVC 2 in Action. The chapter on autocomplete is at the end which I use as reference.
In the controller, the Json results that is returned is not transformed into a list for autocomplete. The book did not use Json but I could not use their alternative with a generic list.
So my View is;
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.WebUI.Models.HolidayRequestViewModel>" %>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("input#SearchText").autocomplete('<%: Url.Action("FindNames", "Employee") %>');
});
</script>
<p>You must make sure that the correct person to approve your Annual Leave is currently selected</p>
<p>Your current approver is <%: Html.DisplayFor(model => model.ApproverName) %></p>
<p>If you want to change your approver, enter his/her name here and make your selection.</p>
<p><%: Html.TextBoxFor(model => model.SearchText) %></p>
<div id="test-panel" class="ui-state-default"> This panel will disappear on command.</div>
And my controller is;
public JsonResult FindNames(string q)
{
List<EmployeeName> filteredEmployees =
Employee.GetAllCurrentEmployeesNames().Where(x => x.Fullname.ToLower().Contains(q.ToLower())).ToList();
return Json(filteredEmployees, JsonRequestBehavior.AllowGet);
}
* EDITED *
The problem with sending the parameter has now been fixed by using “string q”. Obvious eh? Now it is a case of getting the JSON returned into an autocomplete list.
I did a helper for this, you can use it without having to know jQuery at all
look how it works: http://demo.aspnetawesome.com/AutocompleteDemo
you can download the library from here: http://awesome.codeplex.com/