问题描述
在我看来,有在TagBuilder重大突发变化与在公告回购没有提到他们同β7的。
It seems to me that there are major breaking changes in TagBuilder as of beta7 with no mention about them in the announcements repo.
具体的ToString不再呈现tagbuilder,它只是返回的类型的名称。
previously我们可以做我们的HtmlHelper扩展里面像这样的事情,建立嵌套的HTML元素:
Specifically .ToString no longer renders the tagbuilder, it just returns the name of the type.previously we could do things like this inside our HtmlHelper extensions to build up nested html elements:
var li = new TagBuilder("li");
li.AddCssClass("inactive");
var span = new TagBuilder("span");
span.SetInnerText(somestring);
li.InnerHtml = span.ToString();
.InnerHtml现在不再接受字符串,因为它现在是IHtmlContent
.InnerHtml now no longer accepts string because it is now IHtmlContent
但由于的ToString()不会呈现标签这不工作之一:
but since .ToString() doesn't render the tag this doesn't work either:
li.InnerHtml = new HtmlString(span.ToString())
它仅仅呈现为Microsoft.AspNet.Mvc.Rendering.TagBuilder时,类型的名称
it merely renders as "Microsoft.AspNet.Mvc.Rendering.TagBuilder", the name of the type.
我看不到任何TagBuilder新的方法来提供所需的功能。
我在想什么?我怎么能现在构建复杂的嵌套的HTML与TagBuilder?
I don't see any new methods on TagBuilder to provide the needed functionality.What am I missing? How can I build complex nested html with TagBuilder now?
推荐答案
由于 TagBuilder
现在实现 IHtmlContent
,你应该可以直接使用,无需做的ToString()
。
Because TagBuilder
now implements IHtmlContent
, you should be able to use it directly, without doing .ToString()
.
var li = new TagBuilder("li");
li.AddCssClass("inactive");
var span = new TagBuilder("span");
span.SetInnerText(somestring);
li.InnerHtml = span;
在测试当前实现真正的问题7 的是,有两个子标签建设者内容附加到父1没有简单的方法。您可以按照的讨论。
The real problem with the current implementation in Beta 7 is that there is no easy way to append two child tag builder contents to a parent one. You can follow the discussion on GitHub.
目前的建议是让的innerHTML
不可转让,但支持追加
来代替。这是针对于即将实施的测试版8
The current proposal is to make InnerHtml
not assignable, but support Append
instead. This is targeted to be implemented in Beta 8.
在测试解决方法7 是调用 parent.WriteTo
与的StringWriter
将其转换为一个字符串
。
The workaround in Beta 7 is to call parent.WriteTo
with a StringWriter
to convert it to a string
.
这篇关于TagBuilder的innerHTML在ASP.NET MVC 5 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!