Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,810 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
216,810 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. :)