从HTML帮助中的提取物显示名称和描述属性 [英] Extract Display name and description Attribute from within a HTML helper

查看:131
本文介绍了从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);
  ...
}

要能够得到正确的名称属性,我用下面的code:

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

metadata.DisplayName

和对模型视图类我得到的属性:

And on the property of the ModelView class I got :

[DisplayName("Titel")]

的问题是,我还需要描述。有一个属性,叫做显示屏,并且具有名称和说明,但我不知道如何在上面code?

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(见底部的更新,如果您使用的是previous版本)

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的(富)] [显示(NAME =巴)] 在同一个模型属性是多余的,在 [显示] 属性所使用的名称在 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.

更新:

我的previous答案将不会与ASP.NET MVC 2.0。有属性的夫妇,不可能在默认情况下,以填补 DataAnnotations 在.NET 3.5和说明是其中之一。在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帮助中的提取物显示名称和描述属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆