Cannot get list for dropdownfor (to edit model)

69 views Asked by At

I am creating a form to post edits back to the controller and have a few drop downs in the controller. I have tried two different ways that are not working in the view. This is what I tried first:

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

Now I am trying this:

@Html.DropDownListFor(m => m.TechnicianId, ViewBag.TechnicianId)

and this:

@Html.DropDownListFor(m => m.TechnicianId, TechnicianId)

from what I look online the second or third one are the closest. However it is not coming up as a list. Here is the code I have in the controller:

ViewBag.TechnicianId = new SelectList(db.Users.Where(u => u.Status == 1 || u.RoleID == new Guid("00000000-0000-0000-0000-000000000000")), "UserId", "FullName");
1

There are 1 answers

4
rageit On BEST ANSWER

The expression in DropDownListFor should identify the object that contains the properties to display.

Try having users in the TechnicianId list:

ViewBag.TechnicianId = db.Users
       .Where(u => u.Status == 1 || 
                   u.RoleID == new Guid("00000000-0000-0000-0000-000000000000"));

And then in the view:

<%=Html.DropDownListFor(x => x.TechnicianId, 
                             new SelectList(
                                 ViewBag.TechnicianId, 
                                 "Value", 
                                 "Key")) %>