Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC DropDownList用于获取2个名称_Asp.net Mvc_Variables_Html.dropdownlistfor - Fatal编程技术网

Asp.net mvc MVC DropDownList用于获取2个名称

Asp.net mvc MVC DropDownList用于获取2个名称,asp.net-mvc,variables,html.dropdownlistfor,Asp.net Mvc,Variables,Html.dropdownlistfor,嗨,我正在为学校作业开发一个银行系统。您能否在dropdownlist ex中传递2个名称: 这就是模型 public TransferModel(int bankId, int userid) { myAccount = new Service1Client().getAccountByID(bankId); AccountList = new SelectList(new Service1Client().RetrieveAllAccountsByAccountId(user

嗨,我正在为学校作业开发一个银行系统。您能否在dropdownlist ex中传递2个名称:

这就是模型

public TransferModel(int bankId, int userid)
{
    myAccount = new Service1Client().getAccountByID(bankId);
    AccountList = new SelectList(new Service1Client().RetrieveAllAccountsByAccountId(userid), "Id", "Name");
}
我正在将Id、名称设置到选择列表

视图:


退出:model.myAccount.Id,model.AccountList)%>
选择列表将仅显示帐户的名称。是指他们随时可以在下拉列表中显示姓名和余额


谢谢

以下类似的方式将是一种相当整洁的处理方式:

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 */
}

可能重复。请在此处检查答案:
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 */
}
<%: Html.DropDownListFor(model =>  model.SelectedAccountId,
    new SelectList { Model.Accounts, "Id", "AccountSummary" )%>