Reputation: 18869
I'm trying to create a dropdownlist that contains a list of items:
@Html.DropDownList("displayCount", new SelectList(
new[]
{
new SelectListItem { Value = "25", Text = "25" },
new SelectListItem { Value = "50", Text = "50" },
new SelectListItem { Value = "100", Text = "100" },
}
));
When I look at my dropdownlist, it just has 3 options that say "System.Web.Mvc.SelectListItem"
What do I need to do differently here?
Upvotes: 0
Views: 75
Reputation: 26396
The reason why you got "System.Web.Mvc.SelectListItem"
listed in your dropdown is because you didn't specify which attribute of the List Item you want to use for the Text
and Value
of the dropdown list. Right now, it is being displayed based on the .ToString()
of each item in the list, which returns the full name of the SelectListItem
class
@Html.DropDownList("displayCount", new SelectList(
new[]
{
new SelectListItem { Value = "25", Text = "25" },
new SelectListItem { Value = "50", Text = "50" },
new SelectListItem { Value = "100", Text = "100" },
},
"Value",
"Text"
));
Upvotes: 1
Reputation: 37543
Try:
@Html.DropDownList("displayCount", new SelectList(
new[]
{
new SelectListItem { Value = "25", Text = "25" },
new SelectListItem { Value = "50", Text = "50" },
new SelectListItem { Value = "100", Text = "100" },
},
"Value",
"Text"
));
Upvotes: 2
Reputation: 93464
You need to tell the SelectList what fields to use:
new SelectList(..., "Value", "Text")
Upvotes: 2