在我看来,我使用html helper LabelFor
。标签文本有时太长,因此我将max-width
设置为标签。现在看起来像这样:
全文为Label for some fi...
时为Label for some field
因此,在这种情况下,最好显示标签标题以查看全文。
但是standart html帮助程序不会为html添加标题。
我如何使用html助手解决我的问题?
没有HTML Helpers时我需要的示例:http://jsfiddle.net/fdf5kx5z/4/
最佳答案
您可以为此创建一个自定义html帮助器
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace YourAssembly.Html
{
public static class LabelHelpers
{
public static MvcHtmlString TooltipLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
Dictionary<string, object> attributes = new Dictionary<string, object>();
attributes.Add("title", metaData.GetDisplayName());
return helper.LabelFor(expression, attributes);
}
}
}
并添加对
<namespaces>
的web.config
部分的引用<add namespace="YourAssembly.Html "/>