Adding a default SelectListItem

后端 未结 7 1204
一整个雨季
一整个雨季 2020-12-28 18:33
 public IEnumerable GetList(int? ID)
 {
      return from s in db.List
             orderby s.Descript
             select new SelectListItem
          


        
相关标签:
7条回答
  • 2020-12-28 19:03

    This is just for people who are using Webpages framwork (not MVC and webform) with Razor V2 and C#, you need do

        @Html.DropDownList("category", "Please select", listData)
    

    Note: OptionalLabel need to be the middle parameter.

    Equivalent would be add a SelectListitem to your list data:

        var list=db.Query("select id, name from dbtable");
        List<SelectListItem> listData = new List<SelectListItem>();
        listData.Add(new SelectListItem
        {
            Text = "Please select",
            Value = "",
        }); 
    
        foreach(var item in list)   {
            listData.Add(new SelectListItem  {
                Text = item.Name,
                Value = item.id,
                Selected = isSelected 
            });
        }
    
        @Html.DropDownList("play" , listData)
    
    0 讨论(0)
提交回复
热议问题