.NET MVC 强类型助手 - 从属性中获取价值?
我正在创建一个强类型助手(参考:所以问题)。正如下面代码中的“注释”,是否可以以某种方式从表达的属性中获取值,从而绕过可选的 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
I'm creating a strongly typed helper (ref: SO question). As 'commented' in the code below, is it possible to somehow get the value from the expressed property and thus bypass the optional selectedValue
parameter?
<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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想获取表达式指向的相应属性的值,可以使用 FromLambdaExpression 方法:
If you want to get the value of the corresponding property that the expression is pointing to you could use the FromLambdaExpression method: