Asp.net mvc asp.net MVC扩展数据注释

Asp.net mvc asp.net MVC扩展数据注释,asp.net-mvc,data-annotations,Asp.net Mvc,Data Annotations,以及显示名称 [DisplayName("Address line 1 ")] public string Address1{get; set;} Html.LabelFor(model => model.Address1) 我需要显示工具提示,例如 [DisplayName("Address line 1 ")] [ToolTip("The first line of your address as it appears on you bank statement")] publi

以及显示名称

[DisplayName("Address line 1 ")]
public string Address1{get; set;}

Html.LabelFor(model => model.Address1) 
我需要显示工具提示,例如

[DisplayName("Address line 1 ")]
[ToolTip("The first line of your address as it appears on you bank statement")]
public string Address1{get; set;}

Html.LabelFor(model => model.Address1) 
Html.ToolTipFor(model => model.Address1) 
我可以扩展DisplayName DataAnnotation来执行此操作吗?我不知道该怎么做


谢谢

您必须为
Html.LabelFor()
(以及类似内容)提供自己的扩展方法,该方法将考虑工具提示属性。它不一定要从数据注释中派生,因为它将被自定义处理

当然,您可以继承
DisplayName
,然后使用该名称。你会得到什么?您只需提供一个属性,如
DisplayName with tooltip
,它将作为
DisplayName
工作,并且您还可以在代码中使用它来获取工具提示

附加编辑 如果您的工具提示应该通过使用HTML元素的
title
属性来实现,那么我并不是想在
DisplayName
属性中使用一些特殊的字符串语法,而是创建一个继承
displaynamattribute
的新属性类:

public class DisplayNameWithTooltipAttribute: DisplayNameAttribute
{
    public string Tooltip { get; private set; }

    public DisplayNameWithTooltipAttribute(string displayName, string tooltip) : base(displayName)
    {
        this.Tooltip = tooltip;
    }

    ...
}
然后使用自定义属性:

[DisplayNameWithTooltip("Some display name", "Some tooltip")]
public ActionResult SetSomething(SomeObj val) { ... }
public class TooltipAttribute : DescriptionAttribute
{
    public TooltipAttribute()
        : base("")
    {

    }

    public TooltipAttribute(string description)
        : base(description)
    {

    }
}

这样,您就不必解析字符串,其他代码也可以使用它,因为它继承自
DisplayName
(因为代码可能使用调用
IsAssignableFrom
调用和类似调用)。

这应该会让您走上正确的方向。它是一个扩展方法,用于获取要传递的模型属性。(如果您使用的是MVC3,则可以将
MvcHtmlString.Create
替换为
new HtmlString()

Html.ToolTipFor(m=>m.Address1)
公共静态IHtmlString工具提示(此HtmlHelper html,表达式){
MemberExpression ex=(MemberExpression)expression.Body;
foreach(ex.Expression.Type.GetProperty(ex.Member.Name).GetCustomAttributes中的属性(true)){
if(typeof(TooltipAttribute)=attribute.GetType()){
返回MvcHtmlString.Create(((TooltipAttribute)属性).YourTooltipProperty;
}
}
返回MvcHtmlString.Create(“”);
}

我会这样做。是时候参加冠军联赛了,如果你愿意,我明天可以澄清代码

首先是属性:

[DisplayNameWithTooltip("Some display name", "Some tooltip")]
public ActionResult SetSomething(SomeObj val) { ... }
public class TooltipAttribute : DescriptionAttribute
{
    public TooltipAttribute()
        : base("")
    {

    }

    public TooltipAttribute(string description)
        : base(description)
    {

    }
}
然后是一个html帮助程序,允许我们编写html.TooltipFor():

在你看来:

<%= Html.ToolTipFor(x => x.FirstName) %>
x.FirstName)%%>

如果您使用的是元数据类,在我的例子中,由于DB first模型,Alexn helper不会返回Tooltip属性的值,下面的helper会返回

public static MvcHtmlString ToolTipFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    var expressionBody = (MemberExpression)expression.Body;
    Type type = expressionBody.Expression.Type;

    MetadataTypeAttribute[] metadataAttributes = (MetadataTypeAttribute[])type.GetCustomAttributes(typeof(MetadataTypeAttribute), true);
    foreach (MetadataTypeAttribute metadataAttribute in metadataAttributes)
    {
        var metadataPropertyAttributes = metadataAttribute.MetadataClassType.GetProperty(expressionBody.Member.Name).GetCustomAttributes(false);
        foreach (Attribute metadataPropertyAttribute in metadataPropertyAttributes)
        {
            if (typeof(TooltipAttribute) == metadataPropertyAttribute.GetType())
            {
                return MvcHtmlString.Create(((TooltipAttribute)metadataPropertyAttribute).Description);
            }
        }
    }
    return MvcHtmlString.Create("");
}
publicstaticmvchtmlstring工具提示(此HTMLHelperHTML,表达式)
{
var expressionBody=(MemberExpression)expression.Body;
Type Type=expressionBody.Expression.Type;
MetadataTypeAttribute[]metadataAttributes=(MetadataTypeAttribute[])type.GetCustomAttributes(typeof(MetadataTypeAttribute),true;
foreach(metadataAttributes中的MetadataTypeAttribute metadataAttribute)
{
var metadataPropertyAttributes=metadataAttribute.MetadataClassType.GetProperty(expressionBody.Member.Name).GetCustomAttributes(false);
foreach(metadataPropertyAttribute中的属性metadataPropertyAttribute)
{
if(typeof(TooltipAttribute)==metadataPropertyAttribute.GetType())
{
返回MvcHtmlString.Create(((TooltipAttribute)metadataPropertyAttribute).Description);
}
}
}
返回MvcHtmlString.Create(“”);
}

我想我可以看出你的意图,我可以这样做[DisplayName(“地址行1,银行对账单上显示的地址的第一行”)]扩展Html.LabelFor()并在“|”处拆分以获得这两个值。但对我来说,这似乎是一个令人讨厌的恶作剧。你是什么意思?若要澄清,工具提示可能不会直接显示在标签旁边。若要澄清,工具提示可能不会直接显示在标签旁边。我们是否可以将此工具提示文本输入到html属性中,如@html.TextBoxFor(model=>model.FirstName,new{title=model.FirstName.tooltip})??请纠正我的语法。
public static MvcHtmlString ToolTipFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    var expressionBody = (MemberExpression)expression.Body;
    Type type = expressionBody.Expression.Type;

    MetadataTypeAttribute[] metadataAttributes = (MetadataTypeAttribute[])type.GetCustomAttributes(typeof(MetadataTypeAttribute), true);
    foreach (MetadataTypeAttribute metadataAttribute in metadataAttributes)
    {
        var metadataPropertyAttributes = metadataAttribute.MetadataClassType.GetProperty(expressionBody.Member.Name).GetCustomAttributes(false);
        foreach (Attribute metadataPropertyAttribute in metadataPropertyAttributes)
        {
            if (typeof(TooltipAttribute) == metadataPropertyAttribute.GetType())
            {
                return MvcHtmlString.Create(((TooltipAttribute)metadataPropertyAttribute).Description);
            }
        }
    }
    return MvcHtmlString.Create("");
}