0

說明問題結合,來選擇值DropDownListFor內編輯模板

我有一個付款頁面,包括用於輸入銀行帳戶信息的形式。我已將銀行賬戶信息封裝到模型/編輯器模板中。頁面本身有自己的視圖模型,它恰好包含要傳入編輯器的BankAccount屬性。

[[查看模型]]

public class PaymentPageModel { 
    public SomeProperty1 { get; set; } 
    public SomeProperty2 { get; set; } 
    public BankAccount BankAccount { get; set; } 
    ... 
} 

public class BankAccount { 
    public int BankAccountTypeID { get; set; } 
    public string BankName { get; set; } 
    public string ABACode { get; set; } 
    public string AccountNumber { get; set;} 
    public IEnumerable<SelectListItem> BankAccountTypes { 
     get { ... // Constructs the list } 
    } 
} 

[[付款頁面的HTML]]

<% using (Html.BeginForm()) { %> 
<%: Html.EditorFor(m => m.BankAccount) %> 
    ... // Other miscellaneous stuff not related to the actual BankAccount 
<% } %> 

[[編輯模板]

... 
<%: Html.DropDownListFor(m => m.BankAccountTypeID, Model.BankAccountTypes) %> 
... 

問題

最初,這個工作完全當我是直接強鍵入付款頁面到的BankAccount模型。正在填充下拉列表,並且正在選擇模型的正確值。

我最近修改的頁面,它強烈打字到PaymentPageModel,其中包含的BankAccount模型的屬性。 HTML尚未修改。現在的結果是編輯器模板中的所有HTML值都正確填充,除了DropDownList。它是從BankAccountTypes選擇列表中正確綁定值的列表,但所選擇的值沒有約束。我檢查,以確保它應該具有約束力的價值是通過旁邊的DropDownList其輸出設置正確。

這是推動我堅果,並讓我真的懷疑模型綁定和HTML助手的可靠性一般,尤其是如果我無法複雜的視圖模型與編輯模板相結合,封裝呈現/功能。

任何建議,非常感謝。

回答

0

如果你強烈的主視圖中鍵入的編輯模板PaymentPageModel代替:

<%: Html.EditorFor(m => m.BankAccount) %> 

你可以嘗試:

<%: Html.EditorForModel() %> 

,並在編輯器中的模板:

<%: Html.DropDownListFor(m => m.BankAccount.BankAccountTypeID, 
    Model.BankAccount.BankAccountTypes) %> 
+0

感謝您的建議。我會試一試。 – dotariel 2010-10-20 17:34:43