I've written a custom helper:
public static MvcHtmlString TextBoxForWithTooltip<Tmodel, TProperty>(this HtmlHelper<Tmodel> htmlHelper, Expression<Func<Tmodel, TProperty>> expression, object htmlAttributes = null, string tooltip = "")
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (!string.IsNullOrEmpty(metaData.Description))
{
attributes.Add("title", metaData.Description);
}
if (!string.IsNullOrWhiteSpace(tooltip))
{
attributes.Add("data-tooltip", tooltip);
}
if (attributes.ContainsKey("style"))
{
var val = attributes["style"];
attributes["style"] = val + ";border-radius: 6px;";
}
else
{
attributes.Add("style", "border-radius: 6px;");
}
return htmlHelper.TextBoxFor(expression, attributes);
}
And the CSS, which I got from [here][1]. This works great when you assign the data-tooltip to a button or an "a" or "li" element. But not to an input or TextBoxFor. I see the data-tooltip attribute in the page source but it apparently does not trigger the CSS to show the tooltip. Does anyone have an idea why?
Actually what I did was this:
public static MvcHtmlString TextBoxForWithTooltip<Tmodel, TProperty>(this HtmlHelper<Tmodel> htmlHelper, Expression<Func<Tmodel, TProperty>> expression, object htmlAttributes = null, string tooltip = "")
{
IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (attributes.ContainsKey("style"))
{
var val = attributes["style"];
attributes["style"] = val + ";border-radius: 6px;";
}
else
{
attributes.Add("style", "border-radius: 6px;");
}
return MvcHtmlString.Create("<div data-tooltip='" + tooltip + "'>" + htmlHelper.TextBoxFor(expression, attributes) + "</div>");
}
I embedded the html within the DIV to get the tooltip.