我想使用TagBuilder返回以下字符串:<h2>This is my Title</h2>
这是到目前为止我得到的:
var tb = new TagBuilder("h2");
tb. ??How to add text to go in between??
var ret = tb.ToString();
任何帮助表示赞赏
答案 0 :(得分:1)
您可以按照以下步骤创建课程
public static class HeaderTagBuilder
{
public static string Header2(this HtmlHelper helper, string text, string id)
{
return Header2(helper, text,id, null);
}
public static string Header2(this HtmlHelper helper,string text, string id = null, object htmlAttributes = null)
{
// Create tag builder
var builder = new TagBuilder("h2");
//add the text
if (text == null)
text = string.Empty;//just create an empty string
builder.SetInnerText(text);
// Create valid id
builder.GenerateId(id);
// Add attributes
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
// Render tag
return builder.ToString(TagRenderMode.Normal);
}
您缺少的是行
builder.SetInnerText(text);