How to prepopulate drop down box with default custom value Html.DropDownListFor(...)

84 views Asked by At

So I have a prefilled drop down list

@Html.DropDownListFor(a => a.Item2.CategoryName, new SelectList(Model.Item1.Categories))

I want to add a default selection "All Categories" in the drop down list, how can I accomplish that?

2

There are 2 answers

0
Ajeet Kumar On BEST ANSWER

try this

@Html.DropDownListFor(a => a.Item2.CategoryName, new SelectList(Model.Item1.Categories),"All Categories")
0
Keith On

You can actually add your default item in your model list Categories before passing it to your view like this:

List<string> Categories = new List<string>();  
Categories.Add("All Categories");

then add the other items.

Categories.Add("Item1");
Categories.Add("Item2");
Categories.Add("Item3");