我是搖滾我自己定製的HtmlHelper啓用AutoComplete support on a Select List。自定義htmlhelper驗證支持
這是行得通的,除了我需要能夠支持ViewModel中的DataAnnotations。
這裏是我當前的(工作)的HtmlHelper(沒有驗證)
<Extension()>
Public Function AutoCompleteDropDownList(ByVal helper As HtmlHelper, name As String, autoCompleteSelectListItem As List(Of AutoCompleteSelectListItem), htmlAttributes As Object) As MvcHtmlString
Dim selectBuilder As New TagBuilder("select")
selectBuilder.MergeAttribute("name", name)
selectBuilder.MergeAttributes(New RouteValueDictionary(htmlAttributes))
selectBuilder.MergeAttribute("autocorrect", "off")
selectBuilder.MergeAttribute("autocomplete", "off")
Dim selectListBuilder As New TagBuilder("option")
selectListBuilder.MergeAttribute("value", "")
selectListBuilder.MergeAttribute("selected", "selected")
Dim innerHtmlBuilder As New StringBuilder
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
For Each item In autoCompleteSelectListItem
selectListBuilder = New TagBuilder("option")
selectListBuilder.MergeAttribute("value", item.Value)
selectListBuilder.MergeAttribute("data-alternative-spellings", item.AlternativeSpellings)
selectListBuilder.MergeAttribute("data-relevancy-booster", item.RelevancyBooster)
selectListBuilder.InnerHtml = item.Label
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
Next
selectBuilder.InnerHtml = innerHtmlBuilder.ToString()
Return MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal))
End Function
如何將一個改寫這個來支持驗證?像AutoCompleteDropDownListFor()
PS:一個C#解決方案是完全可以接受的,我正在開發的項目是在VB中,但我不介意翻譯。
PS:我是通過源代碼上http://aspnet.codeplex.com翻轉,我無法找到DropDownListFor
感謝達林,這將是聰明,能發送模型而不是字符串?類似於你如何做'TextBoxFor'?如果是這樣,那可能是什麼樣子? –
@ChaseFlorell,是的,使用強類型助手'AutoCompleteDropDownListFor'獲取lambda表達式而不是硬編碼魔術字符串將會好得多。我已經更新了我的答案來舉例說明。 –
光榮!謝謝。 –