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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

131<br />

var autocompleteUrl = '@Url.<strong>Action</strong>("F<strong>in</strong>d")'; <br />

$("<strong>in</strong>put#city").autocomplete({ <br />

source: autocompleteUrl, <br />

m<strong>in</strong>Length: 2, <br />

select: function (event, ui) { <br />

alert("Selected " + ui.item.label);<br />

}<br />

});<br />

});<br />

<br />

Build<br />

E search URL<br />

F Add<br />

autocomplete<br />

behavior<br />

Cities<br />

<br />

Start typ<strong>in</strong>g a city to see<br />

the autocomplete behavior <strong>in</strong> action.<br />

<br />

<br />

City<br />

<br />

<br />

G Conta<strong>in</strong>er<br />

for results<br />

As with our previous examples, we need jQuery. B If you have not customized your<br />

layout, these script references are <strong>in</strong>cluded.<br />

Next we add a reference to the jQuery UI stylesheet C, which also ships with the<br />

default project template. Aga<strong>in</strong>, if you have not customized your layout, you will<br />

already have this.<br />

Follow<strong>in</strong>g this, we <strong>in</strong>clude a script block that runs when the page loads D. We<br />

beg<strong>in</strong> by def<strong>in</strong><strong>in</strong>g a variable called autoCompleteUrl, which conta<strong>in</strong>s the URL of the<br />

F<strong>in</strong>d action of the CitiesController (which we haven’t created yet) E. This is the<br />

URL that will be requested each time the user types a character <strong>in</strong> the box <strong>in</strong> order to<br />

perform the search. We then f<strong>in</strong>d any text boxes on the page with the ID of city and<br />

<strong>in</strong>voke the Autocomplete plug<strong>in</strong> on this element F. We tell it where it should look for<br />

data (<strong>in</strong> this case, our autoCompleteUrl), the m<strong>in</strong>imum number of characters that<br />

have to be entered before search<strong>in</strong>g (<strong>in</strong> this case, 2), and a callback function that<br />

should be <strong>in</strong>voked when the user has selected a search result. For simplicity, we’ll just<br />

pop up an alert with the name of the selected city. F<strong>in</strong>ally, we def<strong>in</strong>e the text box that<br />

will allow the user to perform the search G.<br />

Runn<strong>in</strong>g the page at this po<strong>in</strong>t will display a text box. However, as we haven’t yet<br />

implemented the F<strong>in</strong>d action, it currently produces an error, as shown <strong>in</strong> figure 7.11.<br />

When a search term is entered <strong>in</strong> the box, the Autocomplete plug<strong>in</strong> makes an Ajax<br />

request back to the server. In this case, it is to our F<strong>in</strong>d action and it passes the search<br />

term as a query str<strong>in</strong>g parameter called term. The Autocomplete plug<strong>in</strong> expects this<br />

URL to return an array of JSON objects with the follow<strong>in</strong>g properties: an id, a label<br />

(which will be displayed <strong>in</strong> the search results), and a value (which will be <strong>in</strong>serted<br />

<strong>in</strong>to the text box when clicked).<br />

At the moment, this is caus<strong>in</strong>g a 404 error because we haven’t yet implemented the<br />

F<strong>in</strong>d action. We can now go ahead and do this.

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

Saved successfully!

Ooh no, something went wrong!