1) Required Downloads: Download the following plugins if you don't already have them in your project.
2) Include the JavaScripts and CSS: Here are the files you should include on your page.
<script type="text/javascript" language="javascript" src="<%= Url.Content("~/Content/js/jquery.autocomplete.min.js") %>"></script>
<script type="text/javascript" language="javascript" src="<%= Url.Content("~/Content/js/jquery.validate.min.js") %>"></script>
<link href="<%=Url.Content("~/Content/css/jquery.autocomplete.css")%>" type="text/css" rel="Stylesheet" />
public class ImageController : Controller
{
public ActionResult SearchPerson(string query, int limit)
{if (string.IsNullOrEmpty(query)) return Json(new { PersonId = "-1",
FirstName = string.Empty, MiddleName = string.Empty, LastName = string.Empty });CMSDataContext cms = new CMSDataContext();
var result = cms.SearchByPerson(null, null, query);
var data = from r in result select new { PersonId = r.PersonId.ToString(),
r.FirstName,
MiddleName = string.IsNullOrEmpty(r.MiddleName) ? string.Empty : r.MiddleName,
r.LastName };
return Json(data);}
...
4) Place an HTML input control on the page: The input html will be targeted by the autocomplete jQuery plugin and I'm using a hidden span to store the selected value of the autocomplete input control which is PersonId in this case.
<input id="searchName" size="30" />
<span id="personSearchresult" style="display:none"></span>
<script language="javascript" type="text/javascript">
$(document).ready(function(){$('#searchName').autocomplete('<%=Url.Action("SearchPerson", "Image") %>', {
dataType:"json", formatItem: function(data,i,max,value,term){ return value;},
parse: function(data){var array = new Array();
for(var i=0;i<data.length;i++) {
var tempValue = data[i].MiddleName != "" ? data[i].FirstName + " " + data[i].MiddleName + " " + data[i].LastName : data[i].FirstName + " " + data[i].LastName;
var tempResult = data[i].MiddleName != "" ? data[i].FirstName + " " + data[i].MiddleName + " " + data[i].LastName : data[i].FirstName + " " + data[i].LastName;
array[array.length] = { data:data[i], value: tempValue, result: tempResult};}
return array;}
});
$("#searchName").result(function(event, data, formatted) {
$("#personSearchresult").html( !data ? "-1" : "" + data.PersonId);
});
...
Following these simple steps, you should be able to implement an autocomplete control in your ASP.NET MVC application. Hopefully this post gives you a jump start on Autocomplete jQuery plugin in your MVC application.
Hope this helps,
Faraz

7 comments:
jQuery MVC Framework is a great combination! But I can not have all ;-) It is enought work to lern Silverlight.
Regards
from Switzerland
Peter Loebel
http://www.silverlight-travel.com/blog/
Thank you for your useful post :-)
The result callback never fires for me if a matching result isn't fired. I want to set the associated hidden value to 0 or -1 if the text entered doesn't match.
Nice job on the MVC side of things! If you have a chance, please check out my jQuery autocomplete/combobox/suggest implementation, FlexBox, at http://www.fairwaytech.com/flexbox, and let me know what you think.
Thanks!
Noah Heldman
I just tried this and had to change the parameter on the MVC controller to be 'q' rather than 'query'. It also looks like the option 'datatype' on autocomplete no longer exists.
My mistake it looks as if dataType does exist though is not documented...
There is another very similar example as the above here:
http://blog.schuager.com/2008/09/jquery-autocomplete-json-apsnet-mvc.html
I experienced a colorado Web Design which provides affordable services to its clients. They got intelligent people to help their customers.
Post a Comment