2013-03-07 2 views
1

저는 MVC에 관해 아주 새로워졌습니다. 나는 다음과 같은 모델 클래스가 있습니다MVC4 : 모델의 인터페이스 객체

public class Store 
{ 
    public PriceList PriceListInfo { get; set; } 
    public IStore storeData; 
}  

public class PriceList 
{ 
    public int id { get; set; } 
    public string codice { get; set; } 
}  

public interface IStore 
{ 
[...] 
}  

public class Silo2Store : IStore 
{ 
    public int S2 { get; set; } 
    public int S3 { get; set; } 
} 

을 그리고 난 내보기에이 모델을 사용합니다 :

public ActionResult Customer() 
{ 
    using (Store t = (Store)Session["Store"]) 
    { 
     if (t.PriceListInfo == null) 
     { 
      t.PriceListInfo = new PriceList(); 
     } 
     t.PriceListInfo.codice = "XXX"; 
     return View(t); 
    } 
} 

내가 싶습니다

@model Store 

@Html.TextBoxFor(model => ((Silo2Store)Model.storeData).S3) 

해당 컨트롤러 방법은 내 컨트롤러에서 모델 검색 :

[HttpPost] 
public ActionResult Customer(Store modelStore) 
{ 
    var test = ((Silo2Store)Model.storeData).S3; 
} 

이지만 Model.storeData 속성은 내보기에서 초기화되지 않았습니다. null입니다. 그런 다음 내 컨트롤러에서 값을 검색 할 수 없습니다.

어쨌든 내 모델을 변경해야합니까?

+2

GET 컨트롤러 메소드의 코드를 표시 할 수 있습니까? –

+0

'공공 ActionResult 고객() { 사용 (스토어 t = (스토어) 세션 [ "스토어"]) { 경우 (t.PriceListInfo == NULL) { t.PriceListInfo = 새로운 가격표() ; } t.PriceListInfo.codice = "XXX"; 돌아 가기 뷰 (t); } } ' – gorgonzola

+0

..보기에서 내 모델을 확인할 때 (디버그 모드에서) 개체 "storeData"가 null이 아닙니다. 컨트롤러 측면에서는 NULL입니다. – gorgonzola

답변

1

IStore에 대한 모델 바인더를 직접 정의해야합니다.

this article on MSDN Magazine about MVC Model Binding에서 촬영 :

예를 들어, Microsoft .NET Framework를 객체 지향 원리에 대한 탁월한 지원을 제공에도 불구하고, DefaultModelBinder는 추상 기본 클래스와 인터페이스에 바인딩에 대한 지원을 제공하지 않습니다.

관련 문제