【问题标题】:MVC3 - Two models in one View - Error - How to classifyMVC3 - 一个视图中的两个模型 - 错误 - 如何分类
【发布时间】: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.List1[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


【解决方案1】:

您的属性称为Class1Class2 =>

@Html.DisplayFor(x => x.Class1.Class1Attribute)
@Html.DisplayFor(x => x.Class2.Class2Attribute)  

甚至更好地使用显示模板而不是编写任何循环:

@Html.DisplayForModel()

然后在~/Views/Shared/DisplayTemplates/ParentView

@model ParentView
@Html.DisplayFor(x => x.Class1)
@Html.DisplayFor(x => x.Class2)

~/Views/Shared/DisplayTemplates/Class1.cshtml内部:

@model Class1
@Html.DisplayFor(x => x.Class1Attribute)

~/Views/Shared/DisplayTemplates/Class2.cshtml内部:

@model Class2
@Html.DisplayFor(x => x.Class2Attribute)

【讨论】:

  • 当我进行此更改时,它正在识别属性(不再有红色下划线弯曲)...但我收到此错误消息。传入字典的模型项的类型为“System.Collections.Generic.List1[Project.Models.Class1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Project.Models.ParentView]”。
【解决方案2】:
@Html.DisplayFor(modelItem => item.Class1.Class1Attribute)

您的模型中有多个 ParentViews 吗?如果没有,那么您可以拥有

@model Project.Models.ParentView

然后将您的类属性引用为

@Html.DisplayFor(modelItem => Model.Class1.Class1Attribute)
@Html.DisplayFor(modelItem => Model.Class2.Class2Attribute) 

【讨论】:

  • 当我进行此更改时,它正在识别属性(不再有红色下划线弯曲)...但我收到此错误消息。传入字典的模型项的类型为“System.Collections.Generic.List1[Project.Models.Class1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Project.Models.ParentView]”
【解决方案3】:

我认为你的循环应该如下

@foreach (var item in Model)
{

    @Html.DisplayFor(modelItem => item.Class1.Class1Attribute)

    @Html.DisplayFor(modelItem => item.Class1.Class1Atttribute)    
}

我假设 Class1AttributeClass1 类中的一个属性

【讨论】:

    【解决方案4】:

    使用

     @Html.DisplayFor(modelItem => modelItem.Class1.Class1Attribute)
    

    或者你可以做类似的事情

     @foreach (var item in Model.Class1)
     {
        @Html.DisplayFor(modelItem => item.Class1Attribute1)
    
        @Html.DisplayFor(modelItem => item.Class1Attribute2)    
    
      }
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多