Search code examples
htmlasp.nethtml-helper

Default selected value for Html Helper Dropdownlist


I want to create a dropdown list using ASP.NET HTML Helpers and I'm looking for a way to set the default selected value of the list with a single argument (Like the index of the element selected by default for example).


Solution

  • Well, not directly in the helper but if you use a collection of SelectListItem objects, then it is possible with this:

    List<SelectListItem> items=new List<SelectListItem>();
    
    items.Add(new SelectListItem{Text="Default", Value="-1", Selected=true});
    

    More details can be found on this page.