How to unpack htmlAttributes in a span tag in ASP.NET MVC
I have 开发者_如何转开发htmlAttributes
generated - originally for the purpose of creating a link with the attribute using Html.ActionLink
.
Now, based on some condition, I would like to create a <span>
tag instead and put the attributes in there. Is there anyway it can be done easily ?
Eg: something like:
<span <%= htmlAttributes.Unpack() %> > Some txt </span>
OR
<%= Html.SpanTag("Some txt", htmlAttributes) %>
OR anything similar without wresling too much with the already generated htmlAttribues?
Thanks
You could easily build an Html.Span Extension something like :
public static MvcHtmlString DatePicker(this HtmlHelper htmlHelper, string name, string text, object htmlAttributes)
{
RouteValueDictionary attributes =
htmlAttributes == null ? new RouteValueDictionary()
: new RouteValueDictionary(htmlAttributes);
TagBuilder tagBuilder = new TagBuilder("span");
tagBuilder.MergeAttributes(attributes);
tagBuilder.MergeAttribute("name", name, true);
tagBuilder.GenerateId(name);
tagBuilder.SetInnerText(text);
return MvcHtmlString.Create(tagBuilder.ToString());
}
I didn't test this but should be working
精彩评论