2010-10-13 48 views
7

看起來像其他人有這個問題,但我似乎無法找到解決方案。DefaultModelBinder不綁定嵌套模型

我有2種型號:人& BillingInfo:

public class Person 
{ 
public string Name { get; set;} 
public BillingInfo BillingInfo { get; set; } 
} 

public class BillingInfo 
{ 
public string BillingName { get; set; } 
} 

我試圖把它綁定直入使用DefaultModelBinder我的行動。

public ActionResult DoStuff(Person model) 
{ 
// do stuff 
} 

然而,當Person.Name屬性設置,該BillingInfo始終爲空。

我的帖子是這樣的:

「名稱= statichippo & BillingInfo.BillingName = statichippo」

爲什麼BillingInfo總是空?

回答

5

狀態no repro。你的問題在其他地方,無法確定你所提供的信息。默認的模型綁定器對嵌套類完全正常工作。我用它無限的時間,它一直工作。

型號:

public class Person 
{ 
    public string Name { get; set; } 
    public BillingInfo BillingInfo { get; set; } 
} 

public class BillingInfo 
{ 
    public string BillingName { get; set; } 
} 

控制器:

[HandleError] 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new Person 
     { 
      Name = "statichippo", 
      BillingInfo = new BillingInfo 
      { 
       BillingName = "statichippo" 
      } 
     }; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(Person model) 
    { 
     return View(model); 
    } 
} 

查看:

<% using (Html.BeginForm()) { %> 
    Name: <%: Html.EditorFor(x => x.Name) %> 
    <br/> 
    BillingName: <%: Html.EditorFor(x => x.BillingInfo.BillingName) %> 
    <input type="submit" value="OK" /> 
<% } %> 

發佈的值:Name=statichippo&BillingInfo.BillingName=statichippo在POST操作完美結合。同樣適用於GET。


一種可能的情況下,可能不行如下:

public ActionResult Index(Person billingInfo) 
{ 
    return View(); 
} 

注意動作參數是如何調用billingInfo,相同的名稱BillingInfo財產。確保這不是你的情況。

+0

你說得對。原來我的HTML有一個問題,並輸出: – hackerhasid 2010-10-13 21:56:09

+0

過早輸入;) - 「名稱= statichippo&BillingInfo =&BillingInfo.BillingName = statichippo」 – hackerhasid 2010-10-13 21:56:35

+0

我有同樣的問題,沒有被綁定的嵌套類型。原來我的HTML也有問題。我有2個單選按鈕,其名稱與我的視圖模型上的屬性名稱相同。單選按鈕值也被髮布,所以默認的模型綁定器會感到困惑。 – 2011-04-20 10:02:31

0
public class MyNestedClass 
{ 
    public string Email { get; set; } 
} 

public class LoginModel 
{ 
//If you name the property as 'xmodel'(other than 'model' then it is working ok. 
public MyNestedClass xmodel {get; set;} 

//If you name the property as 'model', then is not working 
public MyNestedClass model {get; set;} 

public string Test { get; set; } 
} 

我有類似的問題。我花了很多時間和偶然發現的問題,我不應該用「模式」的屬性名稱

@Html.TextBoxFor(m => m.xmodel.Email) //This is OK 
@Html.TextBoxFor(m => m.model.Email) //This is not OK 
6

我有這個問題,答案是盯着我的臉了幾個小時。我在這裏包括它,因爲我正在尋找嵌套模型沒有約束力並來到這個答案。

確保您的嵌套模型的屬性與您希望綁定工作的任何模型一樣具有正確的訪問器。

// Will not bind! 
    public string Address1; 
    public string Address2; 
    public string Address3; 
    public string Address4; 
    public string Address5; 


    // Will bind 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string Address3 { get; set; } 
    public string Address4 { get; set; } 
    public string Address5 { get; set; } 
+0

我重新檢查,我的viewmodels也缺乏{get;組; } - 有一種盲目性阻止我們看到這個! – niico 2016-09-19 07:59:52

0

我有同樣的問題,該項目的原開發人員已經爲他沒有使用該視圖模型在回髮帶私人二傳手註冊的屬性。事情是這樣的:

public MyViewModel NestedModel { get; private set; } 

改成這樣:

public MyViewModel NestedModel { get; set; } 
1

這是對我工作。

我改變了這個:

[HttpPost] 
    public ActionResult Index(Person model) 
    { 
     return View(model); 
    } 

要:

[HttpPost] 
    public ActionResult Index(FormCollection fc) 
    { 
     Person model = new Person(); 
     model.BillingInfo.BillingName = fc["BillingInfo.BillingName"] 

     /// Add more lines to complete all properties of model as necessary. 

     return View(model); 
    } 
+0

也爲我工作。感謝您的解決方案。 – Sam 2018-02-15 17:52:11