如何使用编辑器模板中的自定义 htmlhelper 包含完整的 属性 名称

How to include the full property name using a custom htmlhelper from within an editor template

我有一张注册表,我想在其中添加 Html.EditorFor()。

EG:

@ModelType RegisterViewModel 
    <othermarkup />
<div>
    @Html.LabelFor(Function(m) m.ConfirmPassword, New With {.class = "required"})
    <div>
        @Html.PasswordFor(Function(m) m.ConfirmPassword)
        @Html.ValidationMessageFor(Function(m) m.ConfirmPassword)
    </div>
</div>

<div>
    @Html.EditorFor(Function(m) m.AlertSettings)
</div>
<othermarkup />

RegisterViewModel 包含一个名为 AlertSettings(类型为 AlertSettingsViewModel)的 属性,AlertSettingsViewModel.vbhtml 的编辑器模板如下所示:

@ModelType AlertSettingsViewModel
    <othermarkup />
<div class="panel-heading">Personalized Alert Settings</div>
<div class="panel-body">
    <div class="col-sm-6">
        <div>
            <label for="Disciplines">Disciplines</label>
            @Html.ValidationMessageFor(Function(m) m.Disciplines)
            @Html.CheckBoxListFor(Function(m) m.Disciplines, SelectLists.Disciplines())
        </div>

        <div class="visible-xs"><br /></div>
    </div>
</div>
<othermarkup />

AlertSettingsViewModel 的 Disciplines 属性 是一个整数数组。

我正在使用自定义 HtmlHelper 生成复选框列表。这由以下两个方法组成(第一个调用第二个):

''' <summary>
''' Returns a checkbox for each of the provided <paramref name="items"/>.
''' </summary>
<System.Runtime.CompilerServices.Extension>
Public Function CheckBoxListFor(Of TModel, TValue)(htmlHelper As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TValue)), items As IEnumerable(Of SelectListItem), Optional htmlAttributes As Object = Nothing) As MvcHtmlString
    Dim listName = ExpressionHelper.GetExpressionText(expression)
    Dim metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData)

    items = GetCheckboxListWithDefaultValues(metaData.Model, items)
    Return htmlHelper.CheckBoxList(listName, items, htmlAttributes)
End Function




''' <summary>
''' Returns a checkbox for each of the provided <paramref name="items"/>.
''' </summary>
<System.Runtime.CompilerServices.Extension>
Public Function CheckBoxList(htmlHelper As HtmlHelper, listName As String, items As IEnumerable(Of SelectListItem), Optional htmlAttributes As Object = Nothing) As MvcHtmlString
    Dim Result As String = ""
    For Each item In items
        Dim div = New TagBuilder("div")
        div.MergeAttributes(New RouteValueDictionary(htmlAttributes), True)
        div.MergeAttribute("class", "checkbox") ' default class

        Dim label = New TagBuilder("label")

        Dim cb = New TagBuilder("input")
        cb.MergeAttribute("type", "checkbox")
        cb.MergeAttribute("name", listName)
        cb.MergeAttribute("value", If(item.Value, item.Text))
        If item.Selected Then
            cb.MergeAttribute("checked", "checked")
        End If

        label.InnerHtml = cb.ToString(TagRenderMode.SelfClosing) & item.Text

        div.InnerHtml = label.ToString()

        Result &= div.ToString()
    Next

    Return New MvcHtmlString(Result)
End Function

我遇到的问题是,当调用编辑器模板时,最终调用 ExpressionHelper.GetExpressionText(expression) returns 只有直接的 属性 名称(例如 "Disciplines") 而不是上下文中的全名(例如 "AlertSettings.Disciplines")。

我怎样才能得到全名而不是直接的名字?这是我第一次尝试使用编辑器模板,我在为这个特定用例找到示例时遇到了一些困难。

我能够通过替换来纠正问题:

Dim listName = ExpressionHelper.GetExpressionText(expression)

与:

Dim listName = htmlHelper.NameFor(expression).ToString

在 CheckboxListFor 方法中。

希望这对其他人有用。