0

I am trying to list the users in a dropdownlist, this is my controller:

    var user = db.Users.Where(e => e.Roles.All(r => r.RoleId != "1"))
    .OrderBy(e => e.UserName);
    ViewBag.UserId = new SelectList(user,"Id", "UserName");
    return View();

This is my View:

@Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })

I can display the users on the dropdownlist but when I try to post something, ModelState is not being valid and it gives that error.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Can you post the code in your action method that is handling the post. Typically you would create an Enumerable object to contain your key, value store and then bind it to the Razor helper. Have a look at this generic example that is a good solution for what you are trying to achieve [link](http://www.codeproject.com/Tips/682454/MVC-Generic-Dropdownlist-for-Razor) – IsakBosman Aug 09 '14 at 13:47
  • I updated my question, Some binding issues caused to that error, I fixed it but now I have a different problem. –  Aug 09 '14 at 18:19
  • 1
    Do not *EVER* "delete" the text of your question and replace it with a different one on Stack Overflow. We're here to provide a historical record of questions. If you have a new problem, ask a new question. If you fixed the problem, then post your answer below, and mark it as the answer to help other users who might have a similar problem. – Erik Funkenbusch Aug 09 '14 at 19:00

1 Answers1

2

Your SelectList in ViewBag can't be named the same as your property being edited. Change it to something like ViewBag.UserIdChoices, and you'll be okay.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444