问题绑定选中的值给编辑模板内DropDownListFor [英] Problem binding selected value to DropDownListFor inside Editor Template

查看:110
本文介绍了问题绑定选中的值给编辑模板内DropDownListFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明

我有一个支付页面,其中包括用于输入银行帐户信息的形式。我已经封装在银行帐户信息模型/编辑模板。该页面本身有它自己的视图模型,这恰好包含的BankAccount属性在给编辑的传递。

I have a payment page that includes a form for entering bank account information. I have encapsulated the bank account information into a Model / Editor Template. The page itself has its own View Model, which happens to contain a BankAccount property to be passed in to the Editor.

[查看模式]

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]

[[Payment Page 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模式。下拉列表正在正确填充,并正在选择从模型正确的值。

Initially, this worked perfectly when I was strongly-typing the Payment page directly to the BankAccount model. The dropdown list was being populated properly, and the correct value from the model was being selected.

我最近修改的页面,它强烈打字的PaymentPageModel,其中包含的BankAccount模型的属性。在HTML未被修改。结果现在,就是在编辑器模板中的所有HTML值被正确填充,除了DropDownList的。它是从BankAccountTypes选择列表中正确绑定值的列表,但选择的值没有约束。我已经检查,以确保它应该是具有约束力的价值是由旁边输出它DropDownList的正确设置。

I recently modified the page, strongly-typing it to the PaymentPageModel, which contains the BankAccount model as a property. The HTML has not been modified. The result now, is that all the HTML values in the Editor Template are being populated properly, except for the DropDownList. It is binding the list of values properly from the BankAccountTypes select list, but the selected value is NOT being bound. I have checked to make sure that the value it is supposed to be binding to IS set properly by outputting it right next to the DropDownList.

这是我发疯,并让我真的很怀疑模型的可靠性一般约束力和HTML帮手,尤其是如果我无法复杂的视图模型与编辑模板相结合,封装presentation /功能。

This is driving me nuts, and is making me really question the reliability of Model binding and HTML Helpers in general, especially if I am unable to combine complex view models with Editor Templates to encapsulate presentation/functionality.

任何建议都大大AP preciated。

Any suggestions are greatly appreciated.

推荐答案

如果你在你的主视图,而不是有强类型的编辑模板 PaymentPageModel

If you have strongly typed the editor template to PaymentPageModel in your main view instead of:

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

您可以尝试:

<%: Html.EditorForModel() %>

和在编辑器中的模板:

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

这篇关于问题绑定选中的值给编辑模板内DropDownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆