DropdownListFor default value

Is there an easy way to add the "--Please select--" default option to DropDownListFor in MVC 3?

+48
asp.net-mvc-3
Aug 29 '11 at 11:44
source share
4 answers

So, I did something like this:

@Html.DropDownListFor(model => model.Dessert, new SelectList(Model.AvailableDesserts, "DessertID", "DessertName"), "---Select A Dessert ---") 

Everything seems to be working fine. Dessert in my view model is user selected. AvailableDesserts is a collection of them. Hope this helps.

+73
Aug 29 '11 at 11:53 on
source share

I have several extension methods in SelectList

  public static SelectList PreAppend(this SelectList list, string dataTextField, string selectedValue, bool selected=false) { var items = new List<SelectListItem>(); items.Add(new SelectListItem() { Selected = selected, Text = dataTextField, Value = selectedValue }); items.AddRange(list.Items.Cast<SelectListItem>().ToList()); return new SelectList(items, "Value", "Text"); } public static SelectList Append(this SelectList list, string dataTextField, string selectedValue, bool selected=false) { var items = list.Items.Cast<SelectListItem>().ToList(); items.Add(new SelectListItem() { Selected = selected, Text = dataTextField, Value = selectedValue }); return new SelectList(items, "Value", "Text"); } public static SelectList Default(this SelectList list,string DataTextField,string SelectedValue) { return list.PreAppend(DataTextField, SelectedValue, true); } 

Then my razor looks like this:

 @Html.DropDownListFor(m=>m.SelectedState, Model.StateList().Default("Select One","")) 
+14
Aug 29 '11 at 11:53 on
source share

Hi, how about this (if using the DisplayFor method)

 private IEnumerable<SelectListItem> AddDefaultOption(IEnumerable<SelectListItem> list, string dataTextField, string selectedValue) { var items = new List<SelectListItem>(); items.Add(new SelectListItem() { Text = dataTextField, Value = selectedValue}); items.AddRange(list); return items; } 

Then just add this code to your controller

 //lambda expression binding ViewBag.YourList = db.YourTable.Select(x => x).ToList().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.DisplayName.ToString() }); ViewBag.YourList = AddDefaultOption(ViewBag.YourList, "Select One...", "null", true); 

Finally, in the view, you can display the combobox dropdown menu as shown

  <div class="editor-label"> Your Label </div> <div class="editor-field"> @Html.DropDownListFor(model => model.ForeignKey, (IEnumerable<SelectListItem>)ViewBag.YourList) </div> 
+3
Nov 12 '12 at 3:15
source share

I wanted to set a default value for what was passed as an Url Parameter parameter called SiteType:

  <div class="form-group"> @Html.LabelFor(model => model.Type, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(model => model.Type, ChangeOrderSite.SiteTypeNames.Select(s => new SelectListItem { Text = s.Value, Value = s.Key.ToString(), Selected = s.Key.ToString() == Request["SiteType"] }), new { @class = "control-label col-md-2" }) @Html.ValidationMessageFor(model => model.Type) </div> </div> 

My drop-down list is a list of site types.

0
Apr 15 '15 at 21:24
source share



All Articles