I know that there have been a number of questions asked similar to this, but they all seem to be dealing with a page that loads fine and has problems when trying to POST. I'm using scaffolded code in an MVC project with a code first database. The dropdown that's failing is supposed to be filled from a foreign key relationship, there are two other dropdowns using exactly the same code but with different names, and commenting shows that only this one dropdown is bad. This should be on an edit but the page doesn't even load to be submitting data like the other questions do. Both the create and the edit pages do not load.
With all that said, this is the controller:
public ActionResult Edit(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
ViewBag.Title = new SelectList(db.Job_Title, "Id", "Title", person.Title);
ViewBag.Location = new SelectList(db.Locations, "Id", "Name", person.Location);
ViewBag.Manager = new SelectList(db.Managers, "Id", "Name", person.Manager);
return View(person);
}
and this is a section of the corresponding view:
<div class="form-group">
@Html.LabelFor(model => model.Title, "Title", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*FIXME: Commenting the line below will allow the page to load fine*@
@Html.DropDownList("Title", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, "Location", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Location", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Manager, "Manager", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Manager", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Manager, "", new { @class = "text-danger" })
</div>
</div>
I'd love to know why just the title dropdown kills the page while the other 2 are completely fine. I'd also love to know why scaffolded code was generated with an error!