I've been following this tutorial which shows you how to populate a drop down list. However my create page doesn't show a drop down list at all but shows this
This is my drop down list code in my UserController.cs
private void PopulateAdministratorsDropDownList(object selectedAdministrator = null)
{
var administratorQuery = from d in db.Administrators
orderby d.AdministratorTitle
select d;
ViewBag.AdministratorID = new SelectList(administratorQuery, "AdministratorID", "AdministratorTitle", selectedAdministrator);
}
Views\User\Create.cshtml
@model RecreationalServicesTicketingSystem.Models.User
....
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
.... // more controls
<div class="editor-label">
@Html.LabelFor(model => model.AdministratorID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdministratorID)
@Html.ValidationMessageFor(model => model.AdministratorID)
</div>
....
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
UserController.cs
public ActionResult Create()
{
PopulateAdministratorsDropDownList();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "LastName, FirstMidName, EnrollmentDate,AdministratorID, DepartmentID, DepotID")]User user)
{
try
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException /* dex */)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
PopulateAdministratorsDropDownList(user.AdministratorID);
return View(user);
}
private void PopulateAdministratorsDropDownList(object selectedAdministrator = null)
{
var administratorQuery = from d in db.Administrators
orderby d.AdministratorTitle
select d;
ViewBag.AdministratorID = new SelectList(administratorQuery, "AdministratorID", "AdministratorTitle", selectedAdministrator);
}

@Html.DropDownListFor(m => m.AdministratorID, (SelectList)ViewBag.AdministratorList)- not that you need to change the name of theViewBagproperty - it cannot be the same name as the property your binding to.@Html.DropDownListFor(model => model.AdministratorID, (SelectList)ViewBag.AdministratorID)selectedAdministrator) in theSelectListconstructor - its ignored by the HtmlHelper when binding to a model property.