Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialdodders
Python Development Techdegree Graduate 38,679 PointsIssue with Challenge instructions? ASP.Net Forms
The challenge specifically states: Assume that a collection of select list items (of type SelectList) is available via the ViewBag.DepartmentsSelectListItems property.
However an error is raised if you do not explicitly cast ViewBag.DepartmentsSelectListItems to SelectList type.
Am I misunderstanding something, or is this is a mistake in the challenge?
@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>
@Html.LabelFor(m => m.DepartmentId)
@Html.DropDownListFor(m => m.DepartmentId, (SelectList)ViewBag.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
231,236 PointsI believe the point of the instructions mentioning "(of type SelectList)" is so you'll know what type to cast it to. In fact, if you omit the cast, you get this response containing a more explicit hint:
Bummer: Did you cast the ViewBag.DepartmentsSelectListItems property to SelectList?
dodders
Python Development Techdegree Graduate 38,679 Pointsdodders
Python Development Techdegree Graduate 38,679 PointsMy interpretation was that the incoming list would already be of type SelectList.
But yes, as you say the challenge gives an obvious warning.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsYou're right that is already of that type. Casting doesn't change the type, it just lets the compiler know what it is so it can handle it properly.