在 ASP.NET MVC 中使用描述元数据属性

发布于 2024-09-19 23:12:31 字数 818 浏览 4 评论 0 原文

我正在开发一个使用 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2024-09-26 23:12:42

这是我为 C# 编写的更完整的版本,它也支持 htmlAttributes:

public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var description = metadata.Description;

    RouteValueDictionary anonymousObjectToHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

    TagBuilder tagBuilder = new TagBuilder("span");
    tagBuilder.MergeAttributes<string, object>(anonymousObjectToHtmlAttributes);
    tagBuilder.SetInnerText(description);

    return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}

使用方式如下:

[Display(Name = "Style", Description = "Some description of this field")]
public float Style { get; set; }

@Html.DescriptionFor(model => model.Style, new { @class = "help-inline" })

Here is a more complete version I coded for C#, that also supports htmlAttributes:

public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var description = metadata.Description;

    RouteValueDictionary anonymousObjectToHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

    TagBuilder tagBuilder = new TagBuilder("span");
    tagBuilder.MergeAttributes<string, object>(anonymousObjectToHtmlAttributes);
    tagBuilder.SetInnerText(description);

    return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}

Used like this:

[Display(Name = "Style", Description = "Some description of this field")]
public float Style { get; set; }

@Html.DescriptionFor(model => model.Style, new { @class = "help-inline" })
那支青花 2024-09-26 23:12:40

好吧,如果您使用的是 MVC 3,您可以使用 DisplayAttribute 并像这样使用描述参数。

Public Class User
    <Display(Name = "User name", Description = "This is a description")> _
    Public Property Name As String
End Class


<System.Runtime.CompilerServices.Extension> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim attribute = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)

    Return MvcHtmlString.Create(attribute.Description)
End Function

Mvc2

我刚刚进行了一个粗略的测试,看看这是否有效。 (我不经常使用 VB,而是使用在线转换器)没有错误捕获或任何东西,但它会产生您期望的结果。

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString

    Dim ex As MemberExpression = DirectCast(expression.Body, MemberExpression)
    For Each attribute As Attribute In ex.Expression.Type.GetProperty(ex.Member.Name).GetCustomAttributes(True)
        If GetType(System.ComponentModel.DescriptionAttribute) = attribute.[GetType]() Then
            Return MvcHtmlString.Create(DirectCast(attribute, System.ComponentModel.DescriptionAttribute).Description)
        End If
    Next

    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function

另外,

我真的不知道为什么我要这样做,而你可以这样做。 (尽管我认为 MVC 2 可能无法与 DataAnnotations 一起正常工作)

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function

Well, if you're using MVC 3 you can use the DisplayAttribute and just use the Description Parameter like so.

Public Class User
    <Display(Name = "User name", Description = "This is a description")> _
    Public Property Name As String
End Class


<System.Runtime.CompilerServices.Extension> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim attribute = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)

    Return MvcHtmlString.Create(attribute.Description)
End Function

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.

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString

    Dim ex As MemberExpression = DirectCast(expression.Body, MemberExpression)
    For Each attribute As Attribute In ex.Expression.Type.GetProperty(ex.Member.Name).GetCustomAttributes(True)
        If GetType(System.ComponentModel.DescriptionAttribute) = attribute.[GetType]() Then
            Return MvcHtmlString.Create(DirectCast(attribute, System.ComponentModel.DescriptionAttribute).Description)
        End If
    Next

    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function

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)

<System.Runtime.CompilerServices.Extension()> _
Public Shared Function HintFor(Of TModel, TValue)(html As HtmlHelper(Of TModel),     expression As Expression(Of Func(Of TModel, TValue))) As IHtmlString
    Dim x = ModelMetadata.FromLambdaExpression(Of TModel, TValue)(expression, html.ViewData)
    Return MvcHtmlString.Create(x.Description)
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文