描述
我有一个付款页面,其中包含用于输入银行帐户信息的表单。我已将银行帐户信息封装到模型/编辑器模板中。页面本身有自己的视图模型,它恰好包含要传递给编辑器的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尚未修改。结果是,除了DropDownList之外,编辑器模板中的所有HTML值都正确填充。它正在从BankAccountTypes选择列表中正确绑定值列表,但未绑定所选值。我已经检查过确保通过在DropDownList旁边输出它应该正确绑定到IS的值。
这让我疯狂,并且让我真正质疑模型绑定和HTML帮助程序的可靠性,特别是如果我无法将复杂的视图模型与编辑器模板组合在一起来封装表示/功能。
非常感谢任何建议。
答案 0 :(得分:0)
如果您在主视图中将编辑器模板强烈键入PaymentPageModel
,而不是:
<%: Html.EditorFor(m => m.BankAccount) %>
你可以尝试:
<%: Html.EditorForModel() %>
并在您的编辑器模板中:
<%: Html.DropDownListFor(m => m.BankAccount.BankAccountTypeID,
Model.BankAccount.BankAccountTypes) %>