How to set a value for a default parameter using Html.DropDownList

I am using ASP MVC RC1.

The form I use contains a drop-down list that I put into the view using this code.

<%= Html.DropDownList("areaid", (SelectList)ViewData["AreaId"], "Select Area Id")%> 

However, when rendering, this is what I get

 <select id="areaid" name="areaid"> <option value="">Select Area Id</option> <option value="1">Home</option> ... </select> 

I would like the “Select Area ID” parameter to be 0 and mark it as the default one so that it matches other values, and I can check if the area was selected or not. is a required value. AreaId is an integer, so when I currently click on a form without touching the drop-down list at all, MVC complains that "" is not an integer and gives me a binding error.

SO how to set a default value for a parameter and then make it selected in the form?

Thanks Dan

+6
source share
4 answers

I think you have three four options. First, when you create your SelectList or enumerate SelectItemList, add the selection using the label and the default value. The room at the top will make it the default if any other value is not already selected in the model. Secondly, you can create a “selection” (and parameters) “manually” in the view, using a loop to create the parameters. Again, adding your default selection if it is not specified in the model. Third, use the DropDownList extension, but change the value of the first parameter using javascript after the page loads.

You cannot use the DropDownList extension to assign the value of the Label option because it is hardcoded to use string.Empty . Below is the corresponding code snippet from http://www.codeplex.com/aspnet .

  // Make optionLabel the first item that gets rendered. if (optionLabel != null) { listItemBuilder.AppendLine(ListItemToOption(new SelectListItem() { Text = optionLabel, Value = String.Empty, Selected = false })); } 

EDIT . Finally, it is best for your model to accept Nullable and mark it as required with RequiredAttribute. I would recommend using a presentation model, rather than an entity model for the presentation. Since the value is Nullable, an empty string will work fine if sent back without selecting a value. Setting it as the required value will lead to a failure of the model verification with the corresponding message that this value is required. This will allow you to use the DropdownList helper as is.

 public AreaViewModel { [Required] public int? AreaId { get; set; } public IEnumerable<SelectListItem> Areas { get; set; } ... } @Html.DropDownListFor( model => model.AreaId, Model.Areas, "Select Area Id" ) 
+21
source

For MVC3, SelectList has an overload by which you can determine the selected value.

 Function Create() As ViewResult ViewBag.EmployeeId = New SelectList(db.Employees, "Id", "Name", 1) Return View() End Function 

In this case, I know that 1 is the identifier of the default list item that I want, but presumably you could choose the default by request or what your boat ever floats

+3
source

Instead of passing the default item from the definition in the view, you can add "Select Area" data to the 0th index of the list from the controller.

Thus, the data in the selection region has an index value of 0.

0
source

I wanted to use the same SelectList for multiple drop-down lists and did not want to duplicate SelectList in the model, so I just added a new Html extension method that took a value and set the selected item.

 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, string value, IList<SelectListItem> selectList, object htmlAttributes) { IEnumerable<SelectListItem> items = selectList.Select(s => new SelectListItem {Text = s.Text, Value = s.Value, Selected = s.Value == value}); return htmlHelper.DropDownList(name, items, null /* optionLabel */, htmlAttributes); } 
0
source

All Articles