使用ASP呈现带有title属性的SPAN标记?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (97)

添加HTML是可能的title属性设置为输入标记,如下所示:

@Html.TextBoxFor(model => model.Name, new { title = "Customer name" })

获得span标记呈现如下:

<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" })

扫码关注云+社区

领取腾讯云代金券