DropdownListFor default value

前端 未结 5 521
無奈伤痛
無奈伤痛 2020-11-28 06:38

Is there a simple way to add a \"--Please select--\" default option to a DropDownListFor in MVC 3?

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 07:16

    So, I did something like this:

    @Html.DropDownListFor(model => model.Dessert, 
                          new SelectList(Model.AvailableDesserts, "DessertID", "DessertName"),
                          "---Select A Dessert ---")
    

    Seems to work pretty well. Dessert in my viewmodel is the one selected by the user. AvailableDesserts is a collection of ones to pick from. Hope that helps.

提交回复
热议问题