问题描述
我有关于如何连接,因为在的:
I have some doubts about how to concatenate MvcHtmlString instances because of this information found in MSDN :
MvcHtmlString类重新presents一个HTML-CN codeD字符串
不宜带codeD再次
我冒这个险的字符串是HTML-ZH codeD两次使用code这样的时候:
Do I risk that strings are HTML-encoded twice when using code like this:
var label = Html.LabelFor(model => model.Email);
var textbox = Html.TextBoxFor(model => model.Email);
var validation = Html.ValidationMessageFor(model => model.Email);
var result = MvcHtmlString.Create(
label.ToString() + textbox.ToString() + validation.ToString());
(注:这是应该进入一个的HtmlHelper扩展方法,以减少看法code-重复)。
(note: this is supposed to go into an HtmlHelper extension method to reduce code-duplication in views).
推荐答案
您code是正确的。
从MSDN这片段是指编码视图引擎(如.NET 4的.aspx的视图引擎时使用<%:%>
或剃刀视图在MVC 3引擎)不应该再带code中的对象的字符串值。
That snippet from MSDN means that an encoding View Engine (such as the Aspx view engine in .NET 4 when using <%: %>
or the Razor view engine in MVC 3) should not re-encode the string value of the object.
因此,例如:
string s = "<tag>";
var hs = MvcHtmlString.Create(s);
<%: s %> -- outputs "<tag>"
<%: hs %> -- outputs "<tag>"
这篇关于如何连接几个MvcHtmlString实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!