1

問題の解決策を検索しようとしましたが、失敗しました...

Asp.NET MVC 4 Web アプリケーションに次のようなモデルがあります。

public class ModelBase
{
  public string PropertyOne { get; set; }
  public string PropertyTwo { get; set; }
}

public class InheritedModelOne : ModelBase
{
  public string PropertyThree { get; set; }
}

public class InheritedModelTwo : ModelBase
{
  public string PropertyFour { get; set; }
}

コントローラーには 2 つのアクションがあります。

public ActionResult ActionOne([ModelBinder(typeof(MyModelBinder))]ModelBase formData)
{
  ...
}

public ActionResult ActionTwo(InheritedModelTwo inheritedModelTwo)
{
  ...
}

私の問題は、ActionTwo の Action パラメーターで「inheritedModelTwo」という名前を使用すると、プロパティ PropertyFour が正しくバインドされますが、ActionTwo の Action パラメーターで formData という名前を使用すると、プロパティ PropertyOne と PropertyTwo が正しくバインドされますが、プロパティ 4。私がやりたいことは、フォームを投稿するときに、ActionTwo メソッドの InheritedModelTwo パラメーターの 3 つのプロパティすべてを正しくバインドすることです。

詳細情報:

  1. 投稿は同じ JQuery リクエストからのものです。
  2. post からのデータは、2 つの状況で同じです。
  3. この問題の唯一の違いは、私の ActionTwo のパラメーター名です。
  4. ActionTwo のパラメーターに別の名前を指定すると、ModelBase プロパティのみがバインドされます。
  5. 私の本当に悪い英語でごめんなさい。

わかりました。

4

1 に答える 1

0

私があなたを正しく理解していれば...

あなたがやろうとしているのは、基本オブジェクトタイプを使用して、基本オブジェクトから継承するオブジェクトをマップ/バインドすることです。

継承は一方向にしか機能しないため、これは機能しません。

..したがって、パラメーターの型としてInheritingModel TYPE が必要です。

public class ModelBase
{
    public string PropertyOne { get; set; }
    public string PropertyTwo { get; set; }
}

public class InheritedModelOne : ModelBase
{
    public string PropertyThree { get; set; }
}

public class testObject
{ 
    [HttpPost]
    public ActionResult ActionOne(ModelBase formData)
    {
        formData.PropertyOne = "";
        formData.PropertyTwo = "";

        // This is not accessible to ModelBase
        //modelBase.PropertyThree = "";

        return null;
    }
    [HttpPost]
    public ActionResult ActionOne(InheritedModelOne inheritedModelOne)
    {
        // these are from the Base
        inheritedModelOne.PropertyOne = "";
        inheritedModelOne.PropertyTwo = "";

        // This is accessible only in InheritingModel
        inheritedModelOne.PropertyThree = "";

        return null;
    }

}
于 2013-03-22T14:51:10.860 に答える