2013-02-05 2 views
2

View에 여러 모델을 전달해야하며 View를 강력하게 입력하고 싶습니다. (도움을 줄 수 있으면 통과하지 않고이 작업을하고 싶습니다. ViewBag에 대한 내 모델 하나 하나).ASP.NET MVC 4 모델 캡슐화 렌더링보기 오류

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를 설정하고 볼 수있는 모델을 전달합니다. 프로젝트를 컴파일 할 때 수십 개의 오류가 발생합니다 (아래 참조). 어떻게 해결할 수 있습니까? 나는 단순히 내 Views에서 다른 클래스에 캡슐화 된 여러 모델에 액세스 할 수 있기를 원합니다. 미리 감사드립니다. 내가 코딩 트릭이나 패턴을 잃었 경우

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. 
... 

답변

0

용서하지만, 왜 클래스 '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 Function