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

In Report.cshtml update the "Department" field to use a drop down list. Replace the Html.TextBoxFor method call with a

I have no idea how to add a second parameter to this. Google has failed me in my search. Where can i go to read and learn how to do this?

Report.cshtml
@model IssueReporter.Models.Issue

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

// Winging it here, I am assuming a collection of select list items
    var selectListItem() = new [] 
{
      new SelectListItem() {value = "1", Text = "Item 1", Selected = false}
       new SelectListItem() {value = "2", Text = "Item 2", Selected = true}
};

}

<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>

//  OK, changed TextBoxForto DropDownListFor
// I have watched the video often and can not find how to add a second parameter Can someoe offer a reading assignemtn to help me learn a skill I do not have?
// This is day two on this if my frustration is showing I apoligise 
    <div>
        @Html.LabelFor(m => m.DepartmentId)
        @Html.DropDownListFor(m => m.DepartmentId, SelectListItems)
    <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>
}

2 Answers

Steven Parker
Steven Parker
205,587 Points

You did add a second parameter, just not the right one.

The challenge tells you that a collection of select list items "is available via the ViewBag.DepartmentsSelectListItems property." You should use that as the second parameter of Html.DropDownListFor instead of "SelectListItems".

It also says this collection is "of type SelectList", so you might need to use a cast so they are interpreted as the proper type for the function.

Here's what you need. I'm new too and don't always understand all the jargon people give in their answers. But I figured it out.

<div>
        @Html.LabelFor(m => m.DepartmentId)
        @Html.DropDownListFor(m => m.DepartmentId, (SelectList)ViewBag.DepartmentsSelectListItems)
    </div>