Asp.net-Mvc

如何使用預設值創建一個空的下拉列表?

  • October 24, 2017

我想創建一個只有預設值的空下拉列表。我使用以下程式碼:

@Html.DropDownList("parent","--Select Parent--")

但在執行時我看到這個錯誤:

沒有具有鍵“父”的“IEnumerable”類型的 ViewData 項。

我該如何解決?謝謝。

您可以像這樣建構一個空的 DropDownList:

@Html.DropDownList("parent", Enumerable.Empty<SelectListItem>(), "--Select Parent--")

參考:為 Cascade Sub-List 建構一個空的 MVC DropdownListFor

在上面的例子中添加 html 屬性:

   @Html.DropDownList("Idparent", Enumerable.Empty<SelectListItem>(), "Select one...", new {@class="form-control"})                 

引用自:https://stackoverflow.com/questions/15640275