views:

31

answers:

1

I am following ScottGU tutorial : A Simple E-Commerce Storefront Application

http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx

I can't get data from ViewData in my strongly-typed view Categories.

I really don't know what I am doing wrong because I am following ScottGU tutorial.

I am using the latest MVC version 2 and ScottGU tutorial is based on the very first release.

Here is my aspx code :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" CodeBehind="Categories.aspx.cs" Inherits="System.Web.Mvc.ViewPage<List<MyStore.Models.Category>>" %>

<h2>Browse Products</h2>

<ul class = "categorylisting">

    <% foreach (var category in ViewData)
       { %>

        <li>
            <%=Html.ActionLink(category.CategoryName, new string { action = "List", category = category.CategoryName })%>
        </li>
    <% } %>

</ul>

Here is my Controller class :

[ControllerAction]
    public ActionResult Categories()
    {
        List<Category> categories = northwind.GetCategories();

        return View("Categories",categories);

    }

Thanks for helping..

+1  A: 

You need to use the Model property of ViewData e.g. ViewData.Model

So your code becomes

 <% foreach (var category in ViewData.Model)
       { %>

        <li>
            <%=Html.ActionLink(category.CategoryName, new string { action = "List", category = category.CategoryName })%>
        </li>
    <% } %>

ViewData is just a Dictionary where as the Model contains an instance of the Generic type you have set up in the View in your case List

willbt
I tried ViewData.Model : same result. I can't get the category.CategoryName.It says : Does not contain a definition for 'CategoryName'
Bernard Larouche
Thank you very much. I got it.
Bernard Larouche