@Html.DropDownListFor How to add option?

后端 未结 3 1250
[愿得一人]
[愿得一人] 2021-01-03 21:03
@Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles))

The above code creates me a select list just fine. But I want to

相关标签:
3条回答
  • 2021-01-03 21:42
    @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"})
    
    0 讨论(0)
  • 2021-01-03 21:54

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

    0 讨论(0)
  • 2021-01-03 21:57

    By using the proper DropDownListFor overload:

    @Html.DropDownListFor(
        model => model.ZipFile, 
        new SelectList(ViewBag.ZipFiles),
        "-- please select a zip file --"
    )
    
    0 讨论(0)
提交回复
热议问题