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

MVCモデルバインディングに関するMSDNMagazineのこの記事から引用:

たとえば、Microsoft .NET Frameworkはオブジェクト指向の原則を優れた方法でサポートしていますが、DefaultModelBinderは抽象基本クラスおよびインターフェイスへのバインドをサポートしていません。

于 2013-03-07T11:19:32.377 に答える