2011-02-07 5 views
0

사용자 지정 클래스가 MyCustomType 있습니다. 이 클래스에는 bool 유형의 MyCustomProperty 속성과 bool 유형의 MyCustomProperty1 속성이 있습니다.TempData에서 사용자 지정 형식 가져 오기 asp.net mvc 2

내보기에서 MyCustomProperty가 true인지 확인해야합니다.

<%if (TempData[ViewDataConstants.MyCustomTypeKey] != null && ((MyCustomType)TempData[ViewDataConstants.MyCustomTypeKey]).MyCustomProperty %>show some custom content. 

을하지만 어떤 이유 때 내가 m running it I see error message that MyCustomTYpe could not be found are you missing an assembly reference bla-bla-bla. MyCustomType is in my controller it의 공공 및 심지어 뷰에 대한 참조를 추가 확인하는 방법 : 난`는 다음과 같은 일을하고. 그러나 MyCustomType 클래스가 없다는 말은 계속됩니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

흥미 롭다. 내가 Controllers 네임 스페이스에서 Common으로 옮겼을 때 갑자기 효과가 있었다. 여전히 컨트롤러 네임 스페이스에서 작동하지 않는 이유를 알지 못합니다. 두 네임 스페이스 모두 명시 적으로보기에 포함되었습니다.

답변

1

왜 그것이 작동하지 않았는지 전혀 모르겠지만보기에이 코드를 모두 가지고있는 것은 솔직하게 나에게 잘못되었습니다. 어쩌면 그것은 저와 같습니다. Visual Studio는 C# 코드를 뷰에 쓰는 것을 좋아하지 않습니다 :-).

이보기 모델의 속성과 같아야합니다

public class MyViewModel 
{ 
    public bool MyCustomProperty { get; set; } 
} 

및 컨트롤러 내부 :

public ActionResult Foo() 
{ 
    var model = TempData[ViewDataConstants.MyCustomTypeKey] as MyCustomType ?? new MyCustomType(); 
    var viewModel = Mapper.Map<MyCustomType, MyViewModel>(model); 
    return View(viewModel); 
} 

마지막으로보기 내부 : 이제

<% if (Model.MyCustomProperty) { %> 
    show some custom content. 
<% } %> 

당신이 더 이상 보기에 어떤 용도, 주물, ... 필요합니다.

+0

팁 주셔서 감사합니다. 이런 종류의 접근법에 대해 생각해 보았는데, 내 ViewModels가 DTO이고 내 View에서 가져야하는 것을 제시하고 있습니다 (아직 거부하지 않는 것이 좋습니다). ViewData는 어떤 방식 으로든 사용해야합니다. . 그러나이 시나리오에서는 모델 자체 만 사용할 수있는 것으로 보입니다. 내가 생각했던 다른 방법은 ViewData.Model을 사용하는 것이 었습니다. 당신이 그것에 대해 어떻게 생각하십니까? –

관련 문제