MVC DropDownList获取2个名字

时间:2013-04-21 11:56:58

标签: asp.net-mvc variables html.dropdownlistfor

您好我正在为学校作业开发一个银行系统。你可以在下拉列表中传递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>

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

由于

1 个答案:

答案 0 :(得分: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" )%>