Filtering Data with a Selectbox (Chapter 7 P.295)
I've tried to get this to work but I get the following error.
There is no ViewData item with the key 'CategoryID' of type 'IEnumerable<SelectListItem>'.
The Controller looks like this:
public ActionResult ProductByCategory(int id)
{
NorthwindDataContext db = new NorthwindDataContext();
IList<Product> products = (from p in db.Products
where p.CategoryID == id
select p).ToList();
var categories = db.Categories;
ViewData["CategoryID"] = new SelectList(categories, "CategoryID", "CategoryName");
if (Request.IsAjaxRequest())
{
return View("ProductSearchResults", products);
}
else
{
return View("ProductSearch", products);
}
}
and the View looks like this:
<form action="<%=Url.Action("ProductSearch") %>" method="post" id="jform">
<%= Html.DropDownList("CategoryID")%>
<%=Html.TextBox("query",null, new {size=40}) %>
<input type="submit" id="jsubmit" value="Go" />
</form>
Does anyone have a full listing?
|