可以向输入标签添加HTML标题属性,如下所示:
@Html.TextBoxFor(model => model.Name,new { title = "Customer name" })
静态文本有类似的帮助吗?使用@ Html.DisplayFor(model => model.Name)我可以从模型属性渲染文本。如何添加HTML属性,以便我得到如下渲染的跨标签:
<span title="Customer name">ABC</span>
解决方法
自定义html帮助器可能是最整洁的解决方案。
public static MvcHtmlString SpanFor<TModel,TProperty>(this HtmlHelper<TModel> helper,Expression<Func<TModel,TProperty>> expression,object htmlAttributes = null) { var valueGetter = expression.Compile(); var value = valueGetter(helper.ViewData.Model); var span = new TagBuilder("span"); span.MergeAttributes(new RouteValueDictionary(htmlAttributes)); if (value != null) { span.SetInnerText(value.ToString()); } return MvcHtmlString.Create(span.ToString()); }
=>
@Html.SpanFor(model => model.Name,new { title = "Customer name" })