2014-10-09 2 views
4

전체 오류 메시지 :모델 항목 유형 ...의이지만,이 사전은 ... 필요

@model MyClass<Interface1, Interface2> 
:

내보기에
The model item passed into the dictionary is of type 
'MyClass`2[Implementation1,Implementation2]', 
but this dictionary requires a model item of type 
'MyClass`2[Interface1,Interface2]'. 

, 내가 모델 선언이

MyClass에는 클래스이며, 인터페이스 1과 Interface2 내 컨트롤러 액션에 인터페이스

내가 부르고 있습니다 장소 :

012 32,

: 모델 유형입니다 ...

MyClass<Implementation1,Implementation2> 

... 그리고 Implementation1 어떤 방법이 있나요 인터페이스 1Implementation2 구현 Interface2

를 구현 이 오류를 방지하려면 내 모델을 아래에 선언 할 필요없이? MyClass는이 작업을 수행 할 수 없습니다 불변 때문에

@model MyClass<Implementation1, Implementation2> 
+0

당신이 모델을 사용 '할 수없는 이유라도 MyClass의 <인터페이스 1, Interface2> '? – DavidG

답변

3

, 즉 MyClass<Implementation1, Implementation2>하지 MyClass<Interface1, Interface2>, 따라서 오류가 의미합니다.

인터페이스 또는 대리자가 아니기 때문에이 클래스는 공변으로 선언 할 수 없습니다. 당신이 인터페이스를 만들 수 있지만 그것은 out keyword를 사용하여 공변 :

public interface IMyClass<out T1, out T2> 
{ 
    ... 
} 

public class MyClass<T1, T2> : IMyClass<T1, T2> 
{ 
    ... 
} 

보기의 모델을 선언 :

@model IMyClass<Interface1, Interface2> 
+0

감사합니다! 디자인에 문제가있는 것 같습니다. 'Interface1'과'Interface2'는'MyClass'의 속성 유형입니다. 제 견해에서 사용하고 싶습니다. 그리고 IMyClass에서 그것들을 읽기 전용으로 선언 할 수 있기 때문에 htey는 직렬화되지 않을 것입니다! –

+0

@LeopoldoWagner, 이것은 원래의 질문 범위를 벗어납니다. 다른 질문을 올리면 SO 커뮤니티가 도움이 될 것입니다. – Zabavsky

+0

@Zabavsky 그것은 좋은 대답 +1을 알고 좋다. –