自定义htmlhelper并提供验证支持

时间:2011-11-20 23:23:53

标签: asp.net-mvc-3 validation html-helper

我正在使用我自己的自定义HtmlHelper来启用AutoComplete support on a Select List

这是正常的,除了我需要能够在我的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

的任何引用

1 个答案:

答案 0 :(得分:5)

您必须下载source code,您将在mvc3-rtm-sources.zip\mvc3-rtm-sources\mvc3\src\SystemWebMvc\Mvc\Html\SelectExtensions.cs内找到DropDownListFor帮助程序的实现。要启用客户端验证,您必须在下拉列表中发出HTML5 data- *属性。这是通过调用SelectInternal方法在htmlHelper.GetUnobtrusiveValidationAttributes方法结束时完成的。

<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()
    selectBuilder.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(name))

    Return MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal))
End Function

更新:

根据评论部分的要求,以下是帮助器的强类型版本的样子:

<Extension()>
Public Function AutoCompleteDropDownListFor(Of TModel, TProperty)(helper As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TProperty)), autoCompleteSelectListItem As List(Of AutoCompleteSelectListItem), htmlAttributes As Object) As IHtmlString
    Dim name = ExpressionHelper.GetExpressionText(expression)
    Dim fullHtmlFieldName As String = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name)

    Dim selectBuilder = New TagBuilder("select")
    selectBuilder.MergeAttribute("name", fullHtmlFieldName)
    selectBuilder.MergeAttributes(New RouteValueDictionary(htmlAttributes))
    selectBuilder.MergeAttribute("autocorrect", "off")
    selectBuilder.MergeAttribute("autocomplete", "off")

    Dim selectListBuilder = New TagBuilder("option")
    selectListBuilder.MergeAttribute("value", "")
    selectListBuilder.MergeAttribute("selected", "selected")

    Dim innerHtmlBuilder = 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()
    selectBuilder.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(name))

    Return MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal))
End Function