4

I feel this should be simple but haven't found a guide on here that explains the use of dropdownlistfor in MVC.

I have a simple List of Names in a method in a class Users:

public List<string> getUsersFullNames()
    {
        return (from da in db.Dat_Account
                join ra in db.Ref_Account on da.AccountID equals ra.AccountID
                select ra.FirstName + " " + ra.Surname).ToList();
    }

I want to display each of these names in a dropdownlist so that a name can be selected.

I tried to get this working but have had no success.

My controller:

[Authorize]
    public ActionResult ManageUserAccounts()
    {
            ViewBag.UserList = oUsers.getUsersFullNames();
            return View();
    }

My Model:

public class ManageUserAccountsViewModel
{
    [Display(Name = "Users")]
    public List<SelectListItem> UserList { get; set; }
}

My View:

Html.DropDownListFor(model => model.UserList, new SelectList(oUsers.getUsersFullNames(), "Select User"));

I'm quite new to asp.net MVC as I have always used webforms in the past. Has anyone any idea if this is possible or a way to display this?

Thanks,

5
  • 1
    You cannot bind a <select> to List<SelectListItem>. Your model needs a simple property to bind to (the value of the selected option), e.g. public string SelectedUser { get; set; } Commented Jan 26, 2016 at 21:18
  • Could you give me an example? I tried something similar to this earlier with no luck. Thanks Commented Jan 26, 2016 at 21:42
  • Travis Schettler's edit is now correct, except that I suggest that public List<string> UserList { get; set; } be public IEnumerable<SelectListItem> UserList { get; set; } and you use @Html.DropDownListFor(m => m.User, Model.UserList, "Select User") and the [Display] attribute needs to be on User, not UserList Commented Jan 26, 2016 at 22:06
  • Brilliant, thanks for the help Commented Jan 26, 2016 at 22:24
  • Then you should consider accepting Travis' answer :) Commented Jan 26, 2016 at 22:28

1 Answer 1

10

I would recommend using the model directly in the view, instead of the ViewBag. Update your action to include a model reference:

public ActionResult ManageUserAccounts()
{
    var model = new ManageUserAccountsViewModel();
    model.UserList = oUsers.getUsersFullNames();
    return View(model);
}

Your model should be updated to include a selected User property:

public class ManageUserAccountsViewModel
{
    public string User { get; set; }

    [Display(Name = "Users")]
    public List<string> UserList { get; set; }
}

Your view should be binding to the model:

@model ManageUserAccountsViewModel

@Html.DropDownListFor(m => m.User, new SelectList(Model.UserList), "Select User")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for replying, I tried this but still get the same result, the list of names builds fine but nothing is displayed in the view.
Updated with a more complete answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.