Create DropDownListFor from SelectList with default value

前端 未结 5 1249
花落未央
花落未央 2021-01-05 08:33

I have a dropdownlistfor:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, \"id\", \"Description\"),          


        
5条回答
  •  情书的邮戳
    2021-01-05 08:53

    SelectList ProductSizeList = new SelectList(_context.Sizes, "SizeId", "SizeName");
    
    //after you create the selectList you have to create the default select list item            
    SelectListItem AllSize = new SelectListItem("All Size", "0");
    
    // and after this you add this item to the begin of the selectlist
    ViewData["SizeId"] = ProductSizeList.Prepend(AllSize);
    

提交回复
热议问题