How to use a ViewBag to create a dropdownlist?

后端 未结 7 2329
失恋的感觉
失恋的感觉 2020-12-01 07:35

Controller:

public ActionResult Filter()
{
    ViewBag.Accounts = BusinessLayer.AccountManager.Instance.getUserAccounts(HttpContext.User.Identity.Name);
             


        
7条回答
  •  萌比男神i
    2020-12-01 07:58

    Try using @Html.DropDownList instead:

    Account: 
    @Html.DropDownList("accountid", new SelectList(ViewBag.Accounts, "AccountID", "AccountName"))
    

    @Html.DropDownListFor expects a lambda as its first argument, not a string for the ID as you specify.

    Other than that, without knowing what getUserAccounts() consists of, suffice to say it needs to return some sort of collection (IEnumerable for example) that has at least 1 object in it. If it returns null the property in the ViewBag won't have anything.

提交回复
热议问题