Hi Danny,
I see that the authors only checked for the number of elements in the ViewData.Model. However, it seems that they forgot to consider the case when the user requests the search page for the first time (ViewData.Model will be null in that case). Hence, if you replace
Code:
<%if(ViewData.Model.Count > 0) {
with
Code:
<%if(ViewData.Model != null && ViewData.Model.Count > 0) {
, it should work.
Here's the complete listing of ProductSearchResult.aspx partial view:
Code:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<MyMvc.Models.Product>>" %>
<% if (ViewData.Model != null && ViewData.Model.Count > 0)
{%>
<table cellpadding="5">
<tr>
<td><b>Product</b></td>
<td><b>Price</b></td>
</tr>
<%foreach (MyMvc.Models.Product p in ViewData.Model)
{ %>
<tr>
<td><%= Html.Encode(p.ProductName) %></td>
<td><%= p.UnitPrice %></td>
</tr>
<%} %>
</table>
<% } %>
The View which invokes the controller and displays the result looks like this:
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Ajax Search
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<h2>Search AJAX</h2>
<%using (Ajax.BeginForm("ProductSearch",
new AjaxOptions { UpdateTargetId = "results" }))
{ %>
<%= Html.TextBox("query", null, new {size=40}) %>
<input type="submit" />
<%} %>
<div id="results">
<%
Html.RenderPartial("ProductSearchResults", ViewData.Model);
%>
</div>
</asp:Content>
Note that my Product class is defined as MyMvc.Models.Product
Hope this helps!
Let me know if this solves your problem.
S.