0

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!

dlkulp
  • 2,204
  • 5
  • 32
  • 46

1 Answers1

2

The error means that the value of ViewBag.Title is null or not an IEnumerable<SelectListItem>.

This will be happening because your view includes

@{
    ViewBag.Title = ".....";
@

which overwrites the value you set in the GET method.

  • Good catch! You can change the viewbag item name to `ViewBag.TitleList` and update the UI code to use that. `@Html.DropDownList("Title",ViewBag.TitleList as SelectList)` or even better use a view model with property names which makes sense – Shyju Nov 28 '17 at 20:16
  • @Shyju, Except that would not work if your editing an existing object where the the value of `Title` is already set (the first option would always be selected, not the correct one). As always, best to use a view model, and then change the property name to say `Salutation` –  Nov 28 '17 at 20:24
  • @StephenMuecke Alright, so I see that line at the top of the file but I'm a little confused about what the proposed solution is. Since this was scaffolded, I didn't write the view model so where do I need to be looking to make that change? – dlkulp Nov 28 '17 at 20:39
  • Your editing data, so **always** use a view model. (refer [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc)). Your view model will contain a properties (say) `int? JobTitle` with a `[Required]` attribute, and `IEnumerable JobList`, and in the view `@Html.DropDownListFor(m => m.JobTitle, Model.JobList, "Please select", new { ... })` (and ditto for the other properties of the model you need in the view). Do not use data models in your view, especially when editing data. –  Nov 28 '17 at 20:45