我想使用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();
任何帮助都非常感谢
发布于 2018-11-23 23:38:17
您可以如下所示创建类
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(文本);
发布于 2022-08-11 11:35:34
我花了一小段时间才找到这个,但这是“当前”的答案。您可以访问label元素的原始HTML并将其附加到其中。不过,要小心,保持简单,并在可能的地方使用更高级别的函数。
tb.InnerHtml.Append("This is my Title");https://stackoverflow.com/questions/53453673
复制相似问题