How do I set a value for the default option with Html.DropDownList

后端 未结 4 1036
眼角桃花
眼角桃花 2020-12-20 20:20

I\'m using ASP MVC RC1.

A form I\'m using contains a dropdownlist which I have put in a view with this code.

<%= Html.DropDownList(\"areaid\", (Se         


        
相关标签:
4条回答
  • For MVC3, SelectList has an overload whereby you can define the selected value.

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

    In this case, I happen to know that 1 is the id of the default list item I want, but presumably you could select the default via query or what ever floats your boat

    0 讨论(0)
  • 2020-12-20 20:55

    Instead of passing the default item from the definition in the View, You can add the "Select Area" data at the 0th index of the List from the controller.

    This way the Select Area data has an index value of 0.

    0 讨论(0)
  • 2020-12-20 21:00

    I think you have three four options. First when you are building your SelectList or enumerable of SelectItemList, prepend the selection with your option label and default value. Putting it at the top will make it the default if some other value isn't already chosen in the model. Second, you could build the select (and options) "by hand" in the view using a loop to create the options. Again, prepending your default selection if one isn't supplied in the model. Third, use the DropDownList extension, but modify the value of the first option using javascript after the page is loaded.

    It doesn't seem to be possible to use the DropDownList extension to assign a value to an optionLabel as it is hard-coded to use string.Empty. Below is the relevant 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, the best way is to have your model take a Nullable value and mark it as required using the RequiredAttribute. I would recommend using a view-specific model rather than an entity model for the view. Since the value is Nullable, the empty string will work fine if posted back without choosing a value. Setting it as a required value will cause the model validation to fail with an appropriate message that the 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" )
    
    0 讨论(0)
  • 2020-12-20 21:11

    I wanted to use the same SelectList for multiple drop downs and didn't want to duplicate the SelectList in the model so I just added a new Html Extension method that took in 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 讨论(0)
提交回复
热议问题