Asp.net MVC Extends DataAnnotions

As well as DisplayName, for example.

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

Html.LabelFor(model => model.Address1) 

I have a requirement to show tooltips, for example.

[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) 

Can I extend the DataNnotation DisplayName for this? I do not see how this can be done.

Thank!

+4
source share
4 answers

This is how I do it. Time for some Champions League, I can check the code tomorrow if you want.

Attribute first:

public class TooltipAttribute : DescriptionAttribute
{
    public TooltipAttribute()
        : base("")
    {

    }

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

    }
}

And then the html helper so that we can write Html.TooltipFor ():

public static class HtmlHelpers
{
    public static MvcHtmlString ToolTipFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
        var exp = (MemberExpression)expression.Body;
        foreach (Attribute attribute in exp.Expression.Type.GetProperty(ex.Member.Name).GetCustomAttributes(false))
        {
            if (typeof(TooltipAttribute) == attribute.GetType())
            {
                return MvcHtmlString.Create(((TooltipAttribute)attribute).Description);
            }
        }
        return MvcHtmlString.Create("");
    }
}

Then the following will be used:

Your model:

public class User
{
    [Tooltip("This is the attribute for FirstName")]
    public string FirstName { get; set; }
}

And in your opinion:

<%= Html.ToolTipFor(x => x.FirstName) %>
+5
source

Html.LabelFor() ( ), . , .

, DisplayName, . ? , DisplayNameWithTooltip, DisplayName, .

HTML title, - DisplayName, , DisplayNameAttribute:

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) { ... }

, , DisplayName ( , , IsAssignableFrom ..).

+1

. , , . ( MVC3, MvcHtmlString.Create new HtmlString()

Html.ToolTipFor(m => m.Address1)

public static IHtmlString ToolTipFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) {
    MemberExpression ex = (MemberExpression)expression.Body;
    foreach(Attribute attribute in ex.Expression.Type.GetProperty(ex.Member.Name).GetCustomAttributes(true)) {
        if (typeof(TooltipAttribute) == attribute.GetType()) {
            return MvcHtmlString.Create(((TooltipAttribute)attribute).YourTooltipProperty);
        }
    }
    return MvcHtmlString.Create("");

}
+1

, DB-first Alexn 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("");
}
0

All Articles