The problem of binding the selected value to DropDownListFor inside the editor template

Description

I have a payment page that contains a form for entering bank account information. I encapsulated bank account information in a model / editor template. The page itself has its own viewing model, which, as it turned out, contains the BankAccount property, which will be passed to the editor.

[[Show models]]

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 Page]]

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

[[Editor Template]

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

Problem

Initially, this worked great when I strictly printed the "Payment" page directly in the BankAccount model. The drop-down list was filled correctly, and the correct value was selected from the model.

, PaymentPageModel, BankAccount . HTML . , HTML , DropDownList. BankAccountTypes, . , , , IS, , DropDownList.

HTML- , , /.

.

0
1

PaymentPageModel :

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

:

<%: Html.EditorForModel() %>

:

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

All Articles