I am following ScottGU tutorial : A Simple E-Commerce Storefront Application
我正在关注ScottGU教程:一个简单的电子商务店面应用程序
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.
我无法在强类型视图类别中从ViewData获取数据。
I really don't know what I am doing wrong because I am following ScottGU tutorial.
我真的不知道我做错了什么,因为我正在关注ScottGU教程。
I am using the latest MVC version 2 and ScottGU tutorial is based on the very first release.
我正在使用最新的MVC版本2和ScottGU教程基于第一个版本。
Here is my aspx code :
这是我的aspx代码:
<%@ 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 :
这是我的Controller类:
[ControllerAction]
public ActionResult Categories()
{
List<Category> categories = northwind.GetCategories();
return View("Categories",categories);
}
Thanks for helping..
谢谢你帮忙..
1 个解决方案
#1
1
You need to use the Model property of ViewData e.g. ViewData.Model
您需要使用ViewData的Model属性,例如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
ViewData只是一个Dictionary,其中Model包含您在案例列表的View中设置的Generic类型的实例
#1
1
You need to use the Model property of ViewData e.g. ViewData.Model
您需要使用ViewData的Model属性,例如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
ViewData只是一个Dictionary,其中Model包含您在案例列表的View中设置的Generic类型的实例