我需要将多个模型传递给一个视图,并且我希望该视图是强类型的(因此,如果我可以做到这一点,我希望在不使用将每个模型传递给ViewBag的情况下这样做)。
Public Class TestModels
Public Class TestDetail
Public firstModel As firstModelHere ' An entity
Public secondModel As secondModelHere ' An entity
Sub New()
firstModel = Nothing
secondModel = Nothing
End Sub
End Class
End Class我在模型目录中有它自己的独立文件。我将封装的模型传递给我的视图,如下所示:
@ModelType Website.TestModels.TestDetail
@Html.LabelFor(Function(model) model.firstModel.userName)
@Html.LabelFor(Function(model) model.secondModel.lastName)我在控制器中设置了firstModel和secondModel,并将模型传递给视图。当我去编译我的项目时,我得到了几十个错误(见下文),我如何解决这个问题?我只是希望能够从我的视图访问封装在另一个类中的多个模型。提前谢谢。
Error 134 'ViewBag' is not declared. It may be inaccessible due to its protection level.
Error 129 'Context' is not declared. It may be inaccessible due to its protection level.
Error 138 'Layout' is not declared. It may be inaccessible due to its protection level.
Error 131 sub 'Execute' cannot be declared 'Overrides' because it does not override a sub in a base class.
...发布于 2013-02-07 03:47:18
如果我错过了一个编码技巧或模式,请原谅,但为什么在TestModels类中有TestDetail类?
如果您只是希望将多个模型传递给一个视图,我会创建一个单视图模型,并为要传递给视图的每个实体提供一个属性。
视图模型-用于封装要在视图中访问的模型
Public Class TestModel
Public Property FirstModel As FirstEntity ' An entity
Public Property SecondModel As SecondEntity ' An entity
End Class控制器操作
Function Index() As ActionResult
' Create models to pass to the view.
Dim a As New FirstModel
Dim b As New SecondModel
' Create model to pass the models in.
Dim model As New TestModel
With model
.firstModel = a
.secondModel = b
End With
Return View(model)
End Functionhttps://stackoverflow.com/questions/14712433
复制相似问题