View Single Post
  #1 (permalink)  
Old October 13th, 2011, 05:08 PM
JackHerr JackHerr is offline
Authorized User
Points: 108, Level: 2
Points: 108, Level: 2 Points: 108, Level: 2 Points: 108, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Nov 2007
Location: North Salem, NY, USA.
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
Default Chpt 8 Ajax Search Form

I am having trouble getting the Ajax search form in Chpt 8 to work. Instead of the search results appearing in the Home page, as they do for the non-Ajax search in Chpt 5, they appear in a separate/new page with no styling (for which I created no view). The book indicates that the form will replace the UpdateTargetId="searchresults", citing on page 191 that "in this example, you replace an element with the id of searchresults." I did not find anywhere in the example code such an id, and I therefore don't have it in my code. Is this the problem? Do I need to create an element somewhere with this id, and if so, what element and where? I am using a successfully completed Music Store MVC 3.0 tutorial per the .pdf downloaded from the website.

I would appreciate any help you can give me.

Here are the relevant classes, with some slight modifications from the code in the book, with _Layout.cshtml containing working code from the tutorial:

<ahref="/">ASP.NET MVC MUSIC STORE</a></h1>
@{Html.RenderAction("CartSummary", "ShoppingCart");}
@{Html.RenderAction("GenreMenu", "Store");}
built with <ahref="">ASP.NET MVC 3</a>
Index view in Home:
ViewBag.Title = "Index";
This is the Home Page</h2>
@using (Html.BeginForm("Search", "Home", FormMethod.Get))
@using (Ajax.BeginForm("ArtistSearch", "Home", newAjaxOptions
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
OnFailure = "searchFailed",
UpdateTargetId = "searchresults"
ActionResult in Home controller:
publicActionResult ArtistSearch(string q)
var artists = storeDB.Artists.Where(a => a.Name.Contains(q)).ToList();
return PartialView(artists);
ArtistSearch partial view:
@model IEnumerable<MvcMusicStore.Models.Artist>
@foreach (var item in Model)
@Html.DisplayFor(modelItem => item.Name)
Script in MusicScripts.js:
function searchFailed() { $("#searchresults").html("Problem with search."); }