我正在开发一个使用 ASP.NET MVC 2 的 VB.NET 项目。我正在利用向模型中的元数据添加验证和其他属性的功能。
例如,我已将
等属性添加到模型中的属性,并使用 Html.LabelFor ()
渲染这些属性> 扩展方法。
我还向模型中的各种属性添加了
属性,并希望以类似的方式呈现这些属性。
我的问题是 - 是否存在可以为我执行此操作的扩展方法?
如果没有,有人可以引导我朝着正确的方向编写自己的文章吗?我已经基于现有方法的方法签名启动了一个...
<ExtensionAttribute()> _
Public Function HintFor(Of TModel, TProperty) _
(ByVal htmlHelper As HtmlHelper(Of TModel), _
ByVal expression As Expression(Of Func(Of TModel, TProperty))) As MvcHtmlString
End Function
但我必须承认其中的“表达式”部分远远超出了我的 Lambda / LINQ / ??现阶段的知识!!
提前致谢...
I am working on a VB.NET project which is using ASP.NET MVC 2. I am taking advantage of the ability to add validation and other attributes to the metadata in my model.
For example, I have added attributes such as <DisplayName("Full Name")>
to the properties in my model and am rendering these using the Html.LabelFor ()
extension method.
I have also added <Description ("This is a description of the field.")>
attributes to various properties in the model and would like to render these in a similar way.
My question is - does an extension method exist which will do this for me?
If there isn't, could someone steer me in the right direction for writing my own? I've started one based on the method signature of the existing methods...
<ExtensionAttribute()> _
Public Function HintFor(Of TModel, TProperty) _
(ByVal htmlHelper As HtmlHelper(Of TModel), _
ByVal expression As Expression(Of Func(Of TModel, TProperty))) As MvcHtmlString
End Function
But I must admit the 'expression' part of this goes well beyond my Lambda / LINQ / ?? knowledge at this stage!!
Thanks in advance...
发布评论
评论(2)
这是我为 C# 编写的更完整的版本,它也支持 htmlAttributes:
使用方式如下:
Here is a more complete version I coded for C#, that also supports htmlAttributes:
Used like this:
好吧,如果您使用的是 MVC 3,您可以使用
DisplayAttribute
并像这样使用描述参数。Mvc2
我刚刚进行了一个粗略的测试,看看这是否有效。 (我不经常使用 VB,而是使用在线转换器)没有错误捕获或任何东西,但它会产生您期望的结果。
另外,
我真的不知道为什么我要这样做,而你可以这样做。 (尽管我认为 MVC 2 可能无法与 DataAnnotations 一起正常工作)
Well, if you're using MVC 3 you can use the
DisplayAttribute
and just use the Description Parameter like so.Mvc2
I just ran a cursory test to see if this works. (I don't use VB often and used an online converter) There's no error trapping or anything but it will produce the results you expect.
Additional
I'm really not sure why I did the above when you could do it just like this. (though I suppose MVC 2 might not work properly with DataAnnotations)