2012-12-27 30 views
1

我繼承了嘗試的解決方案搜到我的問題,但我不能......MVC 4的參數名稱和視圖模型

我有這樣一個模型,我的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; } 
} 

我在我的控制兩個動作:在我ActionTwo我的行動參數

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

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

我的問題是,當我使用的名稱「inheritedModelTwo」,物業PropertyFour是正確的綁定,但是當我使用名字formData在我的ActionTwo的Action參數中,合適的PropertyOne和PropertyTwo是正確綁定的,但PropertyFour。我想要做的是當我張貼表單時,正確綁定ActionTwo方法的InheritedModelTwo參數的所有三個屬性。

更多信息:

  1. 後來自同一個JQuery的請求。
  2. 來自帖子的數據在兩種情況下是相同的。
  3. 在這個問題上唯一的差異是我的ActionTwo的參數名稱。
  4. 在ActionTwo的參數中放入一個不同的名稱只會使ModelBase屬性綁定。
  5. 對不起,我真的不好英語。

Tks。

+0

你能發表你的看法嗎?你使用@ Html.TextBoxFor幫手嗎?它應該爲你做詭計。另一個技巧,使用小提琴手或Firebug,看看發送給你的控制器。 –

+0

你可以把你的整個視圖與jquery請求?然後我們可以測試你說的。把它放在你的帖子上。 – Sampath

+0

是的,我使用@ Html.TextBoxFor,記住我在兩種情況下發布的數據是一樣的,當我改變ActionTwo的參數名稱時,不同的屬性綁定而沒有視圖更改。 – BetoDR

回答

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

} 
相關問題