2

我正在创建一个强类型助手(参考:SO question)。正如下面代码中的“注释”,是否有可能以某种方式从表示的属性中获取值,从而绕过可选selectedValue参数?

    <Extension()> _
    Public Function DatePickerFor(Of TModel As Class, TProperty)(ByVal htmlHelper As HtmlHelper(Of TModel), ByVal expression As Expression(Of Func(Of TModel, TProperty)), Optional ByVal selectedValue As Nullable(Of Date) = Nothing) As MvcHtmlString
        Dim inputName = ExpressionHelper.GetExpressionText(expression)
        Dim inputValue = selectedValue 
        //Something like this possible? 
        //inputValue = ExpressionHelper.GetExpressionValue(expression)
        Return DatePicker(htmlHelper, inputName, inputValue)
    End Function
4

1 回答 1

9

如果要获取表达式指向的相应属性的值,可以使用FromLambdaExpression方法:

Dim metadata = ModelMetadata.FromLambdaExpression(expression)
Dim value = metadata.Model
于 2011-01-29T08:32:50.407 回答