2012-09-04 45 views
0

我想爲我的下拉列表中定義的默認值按堆棧溢出另一職務爲:錯誤的下拉列表

@Html.DropDownList(Function(model) model.Type, DirectCast(ViewBag.Type, IEnumerable(Of SelectListItem)), "None Selected", "No Payment") 

但我得到的錯誤:

「Lambda表達式不能被轉換爲'String',因爲'String'不是委託類型。「

如何實現這一點,如果沒有選擇任何內容返回「沒有付款」。

回答

0

嗯,首先,我想你必須選擇DropDownListFor,不DropDownList,如果你的第一個參數是Expression(Of Func(Of TModel, TProperty))

在這兩種情況下,有沒有DropDownList/For超載這需要兩個字符串結尾,一個是默認文本另一個是默認值。

如果你

@Html.DropDownList(Function(model) model.Type, DirectCast(ViewBag.Type, IEnumerable(Of SelectListItem)), "None Selected") 

你會得到Model.Type空值時,「無選擇」是已選定的選項。

如果您想完全管理默認值和文本,您必須直接將其添加到您的SelectListItem(如ViewBag.Type)。

並使用

@Html.DropDownList(Function(model) model.Type, DirectCast(ViewBag.Type, IEnumerable(Of SelectListItem))) 

,但不知道它是有用的。有了其他的解決方案,你可以檢查是否選擇了「沒有選擇」類似的東西(我做c#,原諒語法錯誤)

If Model.Type is Nothing Then