ASP.NET MVC查看扔CS1061错误涉及到模型对象传递ViewDdata到一个视图类型 [英] ASP.NET MVC View throwing CS1061 error related to type of model object passed as ViewDdata to a view

查看:185
本文介绍了ASP.NET MVC查看扔CS1061错误涉及到模型对象传递ViewDdata到一个视图类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过ASP.NET MVC的文章,在工作的http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx. (注意:有些在这个职位的结构都是pcated在MVC 1 RTM德$ P $,所以我已经改变了code因此也许这就是我的问题)

I'm working through the ASP.NET MVC article at http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx. (Note: Some of the constructs in this post were deprecated in MVC 1 RTM, so I've changed the code accordingly. Perhaps that's my problem.)

在我的LINQ to SQL的.dbml(中的的MyDB 的了.Designer.cs)的范畴类(编译器生成)的定义如下:

In my LINQ to SQL .dbml (in MyDB.designer.cs) the Category class (compiler generated) is defined as follows:

namespace MVC_Basics_1.Models
{
    ...
    [Table(Name="dbo.Categories")]
    public partial class Category : INotifyPropertyChanging, INotifyPropertyChanged
    {
        ...
    }

在我的控制器类定义了一个分类操作方法如下:

In my controller class I define a Categories action method as follows:

public ActionResult Categories()  // /Products/Categories/ maps here
{
    List<Category> categories = northwind.GetCategories();
    return View("Categories", categories);
}

我然后创建一个Categories.aspx这是强类型为MVC_Basics_1.Models.Category,将在

I then create a Categories.aspx which is strongly typed as "MVC_Basics_1.Models.Category" which places the

<%@ ... Inherits="System.Web.Mvc.ViewPage<MVC_Basics_1.Models.Category>" %>

在文件的第一行。

line at the top of the file.

最后,在.aspx我有:

Finally, in the .aspx I have:

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

两个问题:

首先,当我浏览到/产品/分类/我得到的错误:

First, when I browse to /Products/Categories/ I get the error:

编译器错误信息: CS1061:System.Collections.Generic.KeyValuePair不包含类别名称,没有扩展方法类别名称接受第一类型的参数系统的定义。 Collections.Generic.KeyValuePair'可以找到(是否缺少using指令或程序集引用?)

Compiler Error Message: CS1061: 'System.Collections.Generic.KeyValuePair' does not contain a definition for 'CategoryName' and no extension method 'CategoryName' accepting a first argument of type 'System.Collections.Generic.KeyValuePair' could be found (are you missing a using directive or an assembly reference?)

和类别视图数据对象是不承认任何类别类的属性。

and the category view data object isn't recognizing any of the properties of the Category class.

我在想什么?

二,控制器的类别()操作方法返回的类别(类型为目录类别)作为可视数据的视图列表,但视图的ActionLink的辅助方法,引用category.CategoryName。这是没有道理给我,因为我传递类别(未分类)作为类型的实例。由于控制器的操作方法返回一个列表不这样认为的观点需要被分类为分类?

Second, the controller's Category() action method is returning a list of categories (typed as "List categories") as the viewdata for the view, but the view's ActionLink helper method references a "category.CategoryName". This doesn't make sense to me since I'm passing in an instance of categories (not Category) as the type. Since the controller's action method is returning a List doesn't this suggest the view needs to be typed as Categories?

更新:

我在我的问题,在我的方式来实现这一缺陷。我更注重的错误,我的结果进行比较,以我看比我的实际目标的文章 - 有点像没有看到过的树木森林。当我思考什么,我其实是想完成 - 通过一个模型数据的看法 - 我不再对语法错误的事情,开始思考(一)我想创造在控制器中的特定对象和(B)这片我想这些数据的(S)传递到视图。有一次,我看到过树森林,答案变得明显,我。 @Ufuk和@ryk帮助我认识到这一点。

I realized a flaw in my question and in my approach to this. I was more focused on the error and comparing my results to the article I read than on my actual goal - kind of like not seeing the forest through the trees. When I thought about what I was actually trying to accomplish - passing a model data to the view - I stopped thing about the syntax error and started thinking about (a) the particular object I wanted to create in the controller and (b) which piece(s) of that data I wanted to pass to the view. Once I saw the forest through the trees, the answer became obvious to me. @Ufuk and @ryk helped me realize this.

推荐答案

您应该强类型的视图的列表,而不是模型而已。

You should strongly type the View to the List, not the model only.

<%@ ... Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVC_Basics_1.Models.Category>>" %>

这应该解决这两个问题,你我猜。首先,你不能用一个foreach浏览它becase的模型并没有实现IEnumerable的。第二个名单,其中,分类和GT; 实例列表,您必须参考的成员,如果你想使用范畴类的属性。

This should solve both of your problems I guess. First you couldn't browse it with a foreach becase your model was not implementing IEnumerable. Second List<Category> instances are lists, you have to refer to a member if you want to use Category class' properties.

更新

。在你查看一个问题太多。您发送的列表模式,而不是在ViewData.Change这样你的观点:

There is a problem in your View too. You are sending the list in Model, not in ViewData.Change your View like this:

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

这篇关于ASP.NET MVC查看扔CS1061错误涉及到模型对象传递ViewDdata到一个视图类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆