【发布时间】:2011-09-16 11:24:37
【问题描述】:
我想在一个视图中使用两个模型。为此,我进行了以下设置。 在单个 .cs 文件中,我有以下模型代码:
public class Class1
{
//List of atttributes 1.
}
public class Class2
{
//List of atttributes 2.
}
public class ParentView
{
public Class1 Class1{get; set;}
public Class2 Class2{get; set;}
}
为了在一个视图中使用这两个类,我参考了
@model IEnumerable<Project.Models.ParentView>
在查看 .cshtml 页面中。并且在我的视图代码中有这段代码正在分解..我收到一条错误消息 - “models.parentview 没有 Class1Attribute 的定义,也没有扩展方法......等等。”我该如何分类下面的语句,以便它识别该属性。
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.Class1Attribute)
@Html.DisplayFor(modelItem => item.Class1Atttribute)
}
感谢您的帮助。
【问题讨论】:
-
感谢大家的有用提示。
-
当我进行此更改时,它正在识别属性(不再有红色下划线弯曲)...但我收到此错误消息。传入字典的模型项的类型为“System.Collections.Generic.List
1[Project.Models.Class1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Project.Models.ParentView]”
标签: asp.net-mvc model-view-controller class asp.net-mvc-3 models