@Html.DropDownListFor How to add option?

三世轮回 提交于 2019-11-30 11:02:18

By using the proper DropDownListFor overload:

@Html.DropDownListFor(
    model => model.ZipFile, 
    new SelectList(ViewBag.ZipFiles),
    "-- please select a zip file --"
)
user5093080
@Html.DropDownListFor(model => model.Country, new List<SelectListItem>
   {
     new SelectListItem { Text = "India", Value = "1"},
     new SelectListItem { Text = "USA", Value = "2"},
     new SelectListItem { Text = "Sreelanka", Value = "3"},
     new SelectListItem {Text  = "Africa",Value="4"},
     new SelectListItem { Text = "China", Value = "5" },
     new SelectListItem { Text = "Austraila", Value = "6" },
     new SelectListItem { Text = "UK", Value = "7" }
  }, "Select Country", 
  new {@Style = "Width:500px;height:40px;",
  @class = "form-control input-lg"})

In the controller, when you set ViewBag.ZipFiles, add a SelectListItem to that collection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!