1

我对 MVC 很陌生。我有以下模型类:

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; }
}

我想在我看来使用这个模型:

@model Store

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

对应的Controller方法为:

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

我想在我的控制器中检索模型:

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

Model.storeData在我看来,属性没有初始化,它是null. 然后,我无法检索控制器中的值。

无论如何我应该改变我的模型吗?

4

1 回答 1

1

您必须为IStore.

摘自MSDN 杂志上关于 MVC 模型绑定的这篇文章

例如,尽管 Microsoft .NET Framework 为面向对象的原则提供了出色的支持,但 DefaultModelBinder 不支持绑定到抽象基类和接口。

于 2013-03-07T11:19:32.377 回答