代码之家  ›  专栏  ›  技术社区  ›  Chris Roberts

在中使用描述元数据属性ASP.NETMVC公司

  •  2
  • Chris Roberts  · 技术社区  · 14 年前

    我正在做一件事VB.NET版正在使用的项目ASP.NETMVC 2。我正在利用向模型中的元数据添加验证和其他属性的能力。

    例如,我添加了如下属性 <DisplayName("Full Name")> 并使用 Html.LabelFor () 扩展方法。

    我还补充说 <Description ("This is a description of the field.")> 属性,并希望以类似的方式渲染这些属性。

    我的问题是-是否有一个扩展方法可以为我做到这一点?

    <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/??这个阶段的知识!!

    提前谢谢。。。

    2 回复  |  直到 14 年前
        1
  •  4
  •   Buildstarted    14 年前

    DisplayAttribute 就这样使用Description参数。

    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
    

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

    <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
    
        2
  •  3
  •   sphair    12 年前

    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" })