Home ASP.NET MVC Select Option Value of SelectList
Reply: 0

ASP.NET MVC Select Option Value of SelectList

user5931 Published in September 19, 2018, 7:25 pm

I am rendering a dropdown list.

ViewBag.Categories = new SelectList(db.HelpCategories, "ID", "Name");

In my view I call it like this:

Categories: @Html.DropDownList("Categories", (SelectList)ViewBag.Categories, "All")

For which I get select values like this.

<select id="Categories" name="Categories">
   <option value="">All</option>
   <option value="1">Dėl Krepšelių</option>

I really need to set the value of "All" to 0. Can't figure out a way to do it. Found a tutorial that it's done with SelectListItem, but that doesn't fit my code. Any help is really appreciated, thank you.

share|improve this question
  • Why would you want to set it to 0? And if you do want it then you must generate an IEnumerable<SelectListItem> that incluse one with Value="0" and Text="All" (and delete the 3rd parameter in your DropDownList() method) – Stephen Muecke Nov 10 '17 at 21:15

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330699 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO