2010-09-23 61 views
4

我有一個SelectList從數據庫中獲取數據。數據來自Dictionary<int, string>並像下面那樣填充SelectList。MVC-選擇列表的默認選定值清除

//... 

Courses = new SelectList(GetCourses(),"Key","Value"); 

//... 

在視圖,它顯示了從數據庫上的下拉列表中選擇默認值的第一個記錄。我想清除此值並將下拉列表保留爲空,以便用戶可以手動選擇值。如果可能,不使用JavaScript。

//I have this on the view 
<%= Html.DropDownListFor(model => model.CourseID, Model.Courses) %> 

請幫忙!

回答

9

使用the overload which takes an optionLabel

<%= Html.DropDownListFor(model => model.CourseID, Model.Courses, string.Empty) %> 
+0

感謝。當然,如此明顯。我想是時候辭職了,把我的叔叔帶上那個調酒師的工作。 – 2010-09-23 17:18:13

+0

另請參閱我的DDL教程http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc和http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT 2012-02-14 19:25:34