Asp.net mvc 如何将html实体放入Asp.net mvc html helper编辑器中以获取占位符

Asp.net mvc 如何将html实体放入Asp.net mvc html helper编辑器中以获取占位符,asp.net-mvc,html-helper,asp.net-mvc-helpers,Asp.net Mvc,Html Helper,Asp.net Mvc Helpers,如果我使用原始html和占位符,如: <input id="TextSearch" name="TextSearch" placeholder="&#xf002;" type="text" value="" style="font-family: 'FontAwesome'" /> 它呈现为: 它无法渲染一个可怕的图标。它被视为字符串 如何使用Html helper@Html.EditorFor正确呈现它?您需要为此使用HttpUtility.HtmlDecode,因此您的

如果我使用原始html和占位符,如:

<input id="TextSearch" name="TextSearch" placeholder="&#xf002;" type="text" value="" style="font-family: 'FontAwesome'" />
它呈现为: 它无法渲染一个可怕的图标。它被视为字符串

如何使用Html helper@Html.EditorFor正确呈现它?

您需要为此使用HttpUtility.HtmlDecode,因此您的HTMLHelper应该如下所示

@Html.EditorFor(m => m.TextSearch, new { htmlAttributes = new { @PlaceHolder = HttpUtility.HtmlDecode("&#xf002;"), @Style = "font-family: 'FontAwesome'" } })
为此,您需要使用HttpUtility.HtmlDecode,因此您的HTMLHelper应该如下所示

@Html.EditorFor(m => m.TextSearch, new { htmlAttributes = new { @PlaceHolder = HttpUtility.HtmlDecode("&#xf002;"), @Style = "font-family: 'FontAwesome'" } })
您将占位符和样式HTML属性作为附加的ViewData传递,而不是htmlAttributes。请参阅EditorFor,并使用2个重载。带有HttpUtility.HtmlCode的简单文本框适用于所有MVC版本:

@Html.TextBoxFor(m => m.TextSearch, new { @placeholder = HttpUtility.HtmlDecode("&#xf002;"), @style = "font-family: 'FontAwesome'" })
请注意,在EditorFor中使用htmlAttributes。如果您使用的是MVC版本5.1+,则应使用此编辑器进行设置,否则请使用上文提到的TextBoxFor helper:

@Html.EditorFor(m => m.TextSearch, new { htmlAttributes = new { @placeholder = HttpUtility.HtmlDecode("&#xf002;"), @style = "font-family: 'FontAwesome'" }})
请参阅以查看这两个帮助程序的区别。

您将占位符和样式HTML属性作为附加ViewData传递,而不是htmlAttributes。请参阅带有2个重载的EditorFor。带有HttpUtility.HtmlCode的简单文本框适用于所有MVC版本:

@Html.TextBoxFor(m => m.TextSearch, new { @placeholder = HttpUtility.HtmlDecode("&#xf002;"), @style = "font-family: 'FontAwesome'" })
请注意,在EditorFor中使用htmlAttributes。如果您使用的是MVC版本5.1+,则应使用此编辑器进行设置,否则请使用上文提到的TextBoxFor helper:

@Html.EditorFor(m => m.TextSearch, new { htmlAttributes = new { @placeholder = HttpUtility.HtmlDecode("&#xf002;"), @style = "font-family: 'FontAwesome'" }})

请参阅以查看这两个帮助程序的差异。

能否添加HTML帮助程序呈现的实际HTML以帮助确定根本原因?某些内容正在进行html编码。您能否添加html帮助程序呈现的实际html以帮助确定根本原因?某些内容正在进行html编码。谢谢您的解释。谢谢您的解释。