C# ASP.NET MVC Forms Improving Our Form Drop Down Lists

In Report.cshtml update the "Department" field to use a drop down list.

I have replaced TextBox with DropDownList and added the list as a second parameter. But I am doing something wrong.

Report.cshtml
@model IssueReporter.Models.Issue

@{
    ViewBag.Title = "Report an Issue";
}

<h2>@ViewBag.Title</h2>

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>

    <div>
        @Html.LabelFor(m => m.Email)
        @Html.TextBoxFor(m => m.Email)
    </div>

    <div>
      // replaced TextBox with DropDownList
      // added DepartmentSelectedListItems as a second parameterIn 
        @Html.LabelFor(m => m.DepartmentId)
        @Html.DropDownListFor(m => m.DepartmentId, DepartmentsSelectListItems )
    </div>

    <div>
        @Html.LabelFor(m => m.Severity)
        @Html.TextBoxFor(m => m.Severity)
    </div>

    <div>
        @Html.LabelFor(m => m.Reproducible)
        @Html.TextBoxFor(m => m.Reproducible)
    </div>

    <div>
        @Html.LabelFor(m => m.DescriptionOfProblem)
        @Html.TextAreaFor(m => m.DescriptionOfProblem)
    </div>

    <button type="submit">Save</button>
}

1 Answer

Steven Parker
Steven Parker
205,587 Points

You're close.

The challenge said you could assume the collection of list items was available via the ViewBag.DepartmentsSelectListItems property, but you wrote "DepartmentsSelectListItems" without the "ViewBag.." prefix.

You also might need to cast this collection to the correct type to match the argument (SelectList).

Jon Wood
Jon Wood
9,884 Points

I would do new SelectList instead of cast, but that would probably work, too.

Steven Parker
Steven Parker
205,587 Points

I don't think you can use a new SelectList here. Try plugging it into the challenge and see if it works.

Jon Wood
Jon Wood
9,884 Points

If I recall, I think I was able to in this challenge. This SO answer mentions it, too.

Though, I could be confusing MVC versions.

Edit: It does seem like that the challenge doesn't except new SelectList. I could have sworn that's what I did, but probably not. :)