28.04.2014 Views

ASP.NET MVC 4 in Action - Manning Publications

ASP.NET MVC 4 in Action - Manning Publications

ASP.NET MVC 4 in Action - Manning Publications

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creat<strong>in</strong>g an autocomplete text box<br />

129<br />

Figure 7.10 Google Suggest filters options as you type<br />

7.4 Creat<strong>in</strong>g an autocomplete text box<br />

So far <strong>in</strong> this chapter, you’ve seen how you can leverage Ajax and JSON to make<br />

requests back to the server to retrieve data. In addition to manually issu<strong>in</strong>g these Ajax<br />

requests, you can also make use of client-side control libraries and jQuery plug<strong>in</strong>s that<br />

abstract away much of the boilerplate code for deal<strong>in</strong>g with Ajax requests.<br />

jQuery UI (http://jqueryui.com) is one such set of plug<strong>in</strong>s. It’s built on top of the<br />

jQuery core to provide several client-side user <strong>in</strong>terface widgets, <strong>in</strong>clud<strong>in</strong>g an accordion,<br />

an autocomplete text box, themeable buttons, a datepicker, a modal dialog, a<br />

progress bar, slider, and tabs. In this example, we’ll look at how we can make use of<br />

the Autocomplete plug<strong>in</strong> to present the user with a searchable list of cities, someth<strong>in</strong>g<br />

like Google’s suggest functionality, shown <strong>in</strong> figure 7.10.<br />

7.4.1 Build<strong>in</strong>g the CitiesController<br />

To beg<strong>in</strong>, we’ll create a CitiesController that will render a page conta<strong>in</strong><strong>in</strong>g our text<br />

box, as follows.<br />

List<strong>in</strong>g 7.22 The CitiesController<br />

public class CitiesController : Controller<br />

{<br />

private readonly CityRepository _repository;<br />

public CitiesController()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!