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