0

I have the following in my view at the moment:

@Html.PasswordFor(m => m.Password, new { Title = "Passwords must be at least 6 characters long" })

Works fine, but what's nagging at me is that it would be really great to be able to get the tooltip in the view model as an attribute, as all the other info is there, and it seems messy to put it in my view.

Is there any way that I can pull the value of the attribute through so that my code is something like:

@Html.PasswordFor(m => m.Password, new { Title = m.Password.Tooltip })

Please note: it's not a validation message that should show / hide based on input, think of it as help text.

3
  • I would do this via jQuery. The jQuery UI just released a new version with tooltip support. See jqueryui.com/tooltip Commented Oct 24, 2012 at 19:23
  • This is very closely related to another SO question from today. stackoverflow.com/questions/13052836/… Commented Oct 24, 2012 at 19:40
  • Why does the Password Tooltip have to be a member of the Password property? Can't you create a model.PasswordTooltip property directly on the model? You may want to put someting like this in a Resource Resx file for future localization. Commented Oct 24, 2012 at 21:54

1 Answer 1

2

You could create your own attribute that adds a tool tip to the Model Metadata, and then have a custom helper method render the appropriate html.

e.g.

The attribute

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class ToolTipAttribute : Attribute, IMetadataAware
{
    public string Message { get; set; }

    public ToolTipAttribute(string message)
    {
        Message = message;
    }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues["ToolTip"] = Message;
    }
}

The Helper method

public static MvcHtmlString PasswordForWithToolTip<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper,
                                                                 Expression<Func<TModel, TEnum>> expression)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var htmlAttributes = new Dictionary<string, object>();
    if (metadata.AdditionalValues.ContainsKey("ToolTip"))
    {
        var toolTip = metadata.AdditionalValues["ToolTip"] as string;

        if (!string.IsNullOrEmpty(toolTip))
                htmlAttributes.Add("title", toolTip);
    }

    return htmlHelper.PasswordFor(expression, htmlAttributes);
}

Then, decorate your property with the ToolTip attribute, and use @Html.PasswordFor WithToolTip( x => x.Password) and it should render the appropriate HTML.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. One small thing though. I think objectsAsDict should be htmlAttributes in your example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.