3

嗨,我正在为学校作业开发银行系统。您能否在下拉列表中传递 2 个名称,例如:

这是模型

public TransferModel(int bankId, int userid)
{
    myAccount = new Service1Client().getAccountByID(bankId);
    AccountList = new SelectList(new Service1Client().RetrieveAllAccountsByAccountId(userid), "Id", "Name");
}

我正在将 Id、Name 设置为选择列表

看法:

<div class="editor-field">
    Withdraw From: <%: Html.DropDownListFor(model =>  model.myAccount.Id, Model.AccountList)%>
</div>

选择列表将仅显示帐户名称。是他们有机会在下拉列表中显示名称和余额。

谢谢

4

1 回答 1

2

像下面这样的东西将是一种非常整洁的处理方式:

public class AccountModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Decimal Balance { get; set; }

    public string AccountSummary
    {
       get
       {
           return String.Format("{0} ({1:C})", Name, Balance);
       }
    }
}

public class TransferModel
{
    public IList<AccountModel> Accounts { get; set; }
    public string SelectedAccountId { get; set; }
    /* Whatever other properties your view needs */
}

键入您的视图TransferModel并执行以下操作:

<%: Html.DropDownListFor(model =>  model.SelectedAccountId,
    new SelectList { Model.Accounts, "Id", "AccountSummary" )%>
于 2013-04-21T12:54:47.607 回答