渲染span标记与ASP.NET MVC 3帮手title属性 [英] Render span tag with title attribute with ASP.NET MVC 3 Helpers
本文介绍了渲染span标记与ASP.NET MVC 3帮手title属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有可能增加一个HTML 标题
属性,像这样的输入标签:
@ Html.TextBoxFor(型号=> model.Name,新的{标题=客户名称})
有没有类似的帮手静态文本?随着 @ Html.DisplayFor(型号=> model.Name)
我可以呈现从模型属性的文本。如何添加HTML属性,使我获得了跨度
标记呈现:
<跨度标题=客户名称> ABC< / SPAN>
解决方案
自定义HTML帮助可能是最巧妙的解决方案。
公共静态MvcHtmlString SpanFor<的TModel,TProperty>(此的HtmlHelper<的TModel>的帮手,防爆pression< Func键<的TModel,TProperty>>前pression,对象htmlAttributes = NULL)
{
VAR valueGetter =前pression.Compile();
VAR值= valueGetter(helper.ViewData.Model); VAR跨度=新TagBuilder(跨度);
span.MergeAttributes(新RouteValueDictionary(htmlAttributes));
如果(值!= NULL)
{
span.SetInnerText(value.ToString());
} 返回MvcHtmlString.Create(span.ToString());
}
=>
@ Html.SpanFor(型号=> model.Name,新的{标题=客户名称})
It's possible to add a HTML title
attribute to an input tag like so:
@Html.TextBoxFor(model => model.Name, new { title = "Customer name" })
Is there a similar helper for static text? With @Html.DisplayFor(model => model.Name)
I can render the text from a model property. How can I add HTML attributes so that I get a span
tag rendered like this:
<span title="Customer name">ABC</span>
解决方案
Custom html helper is probably the neatest solution.
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" })
这篇关于渲染span标记与ASP.NET MVC 3帮手title属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文