从 HTML 帮助器中提取显示名称和描述属性

Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)

本文介绍了从 HTML 帮助器中提取显示名称和描述属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个自定义 HTML.LabelFor 帮助器,如下所示:

I am building a custom HTML.LabelFor helper that looks like this :

public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip)
{
  var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
  ...
}

为了能够获得属性的正确名称,我使用以下代码:

To be able to get the proper name for the property I am using the following code :

metadata.DisplayName

关于我得到的 ModelView 类的属性:

And on the property of the ModelView class I got :

[DisplayName("Titel")]

问题是我还需要一个描述.有一个名为 Display 的属性,它具有名称和描述,但我看不到如何使用上述代码中的元数据变量来提取它?

The problem is that I also need a description. There is an Attribute called Display and that has Name and Description but I do not see how to extract this with the metadata variable in the above code?

推荐答案

免责声明:以下内容仅适用于 ASP.NET MVC 3(如果您使用的是旧版本,请参阅底部的更新)

Disclaimer: The following works only with ASP.NET MVC 3 (see the update at the bottom if you are using previous versions)

假设以下模型:

public class MyViewModel
{
    [Display(Description = "some description", Name = "some name")]
    public string SomeProperty { get; set; }
}

还有以下观点:

<%= Html.LabelFor(x => x.SomeProperty, true) %>

在您的自定义助手中,您可以从元数据中获取此信息:

Inside your custom helper you could fetch this information from the metadata:

public static MvcHtmlString LabelFor<TModel, TValue>(
    this HtmlHelper<TModel> self, 
    Expression<Func<TModel, TValue>> expression, 
    bool showToolTip
)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
    var description = metadata.Description; // will equal "some description"
    var name = metadata.DisplayName; // will equal "some name"
    // TODO: do something with the name and the description
    ...
}

备注:在同一个模型属性上有 [DisplayName("foo")][Display(Name = "bar")] 是多余的,使用的名称[Display] 属性中的优先级在 metadata.DisplayName 中.

Remark: Having [DisplayName("foo")] and [Display(Name = "bar")] on the same model property is redundant and the name used in the [Display] attribute has precedence in metadata.DisplayName.

更新:

我之前的回答不适用于 ASP.NET MVC 2.0.在 .NET 3.5 中,默认情况下无法使用 DataAnnotations 填充几个属性,而 Description 就是其中之一.要在 ASP.NET MVC 2.0 中实现这一点,您可以使用自定义模型元数据提供程序:

My previous answer won't work with ASP.NET MVC 2.0. There are a couples of properties that it is not possible to fill by default with DataAnnotations in .NET 3.5, and Description is one of them. To achieve this in ASP.NET MVC 2.0 you could use a custom model metadata provider:

public class DisplayMetaDataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(
        IEnumerable<Attribute> attributes, 
        Type containerType,
        Func<object> modelAccessor, 
        Type modelType, 
        string propertyName
    )
    {
        var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

        var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
        if (displayAttribute != null)
        {
            metadata.Description = displayAttribute.Description;
            metadata.DisplayName = displayAttribute.Name;
        }
        return metadata;
    }
}

您将在 Application_Start 中注册:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    ModelMetadataProviders.Current = new DisplayMetaDataProvider();
}

然后助手应该按预期工作:

and then the helper should work as expected:

public static MvcHtmlString LabelFor<TModel, TValue>(
    this HtmlHelper<TModel> self, 
    Expression<Func<TModel, TValue>> expression, 
    bool showToolTip
)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
    var description = metadata.Description; // will equal "some description"
    var name = metadata.DisplayName; // will equal "some name"
    // TODO: do something with the name and the description
    ...
}

这篇关于从 HTML 帮助器中提取显示名称和描述属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 HTML 帮助器中提取显示名称和描述属性

基础教程推荐