asp.net mvc How to add placeholder for html.dropdownlist

后端 未结 10 576
眼角桃花
眼角桃花 2021-01-01 23:13

I\'m using asp.net mvc 3.

I have this dropdownlist of countries and I want to add a placeholder for it. Here\'s my code:

@Html.DropDownList(\"country         


        
10条回答
  •  难免孤独
    2021-01-01 23:54

    In your collection ViewBag.Countries just insert a dummy record at the from of the collection with the name "-select-". You should be able to force the selected item with an alternate constructor like this:

    @Html.DropDownList("country",
         new SelectList(ViewBag.countries as System.Collections.IEnumerable,
         "name", "name", "-select-"), new { @class="chzn-select", @style="width:160px;" } )
    

提交回复
热议问题