无法使用相结合的标签TagBuilder [英] Unable to combine tags using TagBuilder

查看:312
本文介绍了无法使用相结合的标签TagBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个组合标记:

I'm trying to build a combination tag:

第一个标签:<跨度类=requiredInidicator> * LT; / SPAN>
第二个标签:<标签>&SomeText LT; /标签> (剪断为了简洁属性)

First tag: <span class="requiredInidicator">* </span> Second tag: <label>SomeText</label> (Attributes snipped for brevity)

我想结合这些返回一个MVCHtmlString但以下code完全忽略跨度。可能有人指出,
我在做什么错

I would like to combine these to return an MVCHtmlString but the following code ignores the span completely. Could someone point out what I am doing wrong

下面是我的code:

// Create the Label
var tagBuilder = new TagBuilder("label");
tagBuilder.Attributes.Add("id", "required" + id);
//I get the id earlier fyi - not pertinent fyi for this question )

// Create the Span
var required = new TagBuilder("span");
required.AddCssClass("requiredInidicator"); 
required.SetInnerText("* ");

//Now combine the span's content with the label tag
tagBuilder.InnerHtml += required.ToString(TagRenderMode.Normal);
tagBuilder.SetInnerText(labelText);
var tag = MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
return tag;

在创建标签时,的范围的完全被忽略。当我检查调试期间的标签,它并不关心所需要的,我通过 .InnerHtml + =

When the tag is created, the span is ignored completely. When I inspect the tag during debug, it doesn't care about the required that I appended via .InnerHtml+=

有什么明显我做错了?

推荐答案

尝试这样的:

var label = new TagBuilder("label");
label.Attributes.Add("id", "required" + id);
//I get the id earlier fyi - not pertinent fyi for this question )

// Create the Span
var span = new TagBuilder("span");
span.AddCssClass("requiredInidicator");
span.SetInnerText("* ");

//Now combine the span's content with the label tag
label.InnerHtml = span.ToString(TagRenderMode.Normal) + htmlHelper.Encode(labelText);
return MvcHtmlString.Create(label.ToString(TagRenderMode.Normal));

这篇关于无法使用相结合的标签TagBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆