Мой ответ на попытку. Это работает.
Сценарий
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
// Safely inject CSS3 and give the search results a shadow
var cssObj = { 'box-shadow' : '#888 5px 10px 10px', // Added when CSS3 is standard
'-webkit-box-shadow' : '#888 5px 10px 10px', // Safari
'-moz-box-shadow' : '#888 5px 10px 10px'}; // Firefox 3.5+
$("#suggestions").css(cssObj);
// Fade out the suggestions box when not active
$("input").blur(function(){
$('#suggestions').fadeOut();
});
});
function lookup(inputString) {
if(inputString.length == 0) {
$('#suggestions').fadeOut(); // Hide the suggestions box
} else {
$.post("/Store/Search", { videoTitle: inputString },
function (data) { // Do an AJAX call
$('#suggestions').fadeIn(); // Show the suggestions box
$('#suggestions').html(data); // Fill the suggestions box
});
}
}
Контроллер
[HttpPost]
public JsonResult Search(string videoTitle)
{
List<Searchable> searchedList = new List<Searchable>();
var auth = new Authentication() { Email = "smu@smu.com", Password = "test" };
var videoList = server.Search(auth, videoTitle);
return Json(videoList.ToList(), JsonRequestBehavior.AllowGet);
}
Вид
<form id="searchform">
<div>
What are you looking for?
<input type="text" size="30" id="inputString" name="inputString" onChange="lookup(this.value);" />
</div>
<div id="suggestions">
</div>
</form>
Нет модели, потому что я использую метод веб-сервиса Search, который возвращает список объектов.
Спасибо, надеюсь, что приведенные выше коды помогут всем, кто хочет сделать то же самое.