asp.net-mvchtml-helperasp.net-mvc-helpers

How to put html entity in Asp.net mvc Html helper EditorFor placeholder


If i use raw html <input /> with placeholder like:

<input id="TextSearch" name="TextSearch" placeholder="&#xf002;" type="text" value="" style="font-family: 'FontAwesome'" />

it renders like: enter image description here that is fine. But if i use Asp.net mvc Html helper like:

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

it renders like: enter image description here it can't render FontAwesome icon. It treats as string.

How can i render it correctly using Html helper @Html.EditorFor() ?


Solution

  • You need to used HttpUtility.HtmlDecode for this, so your HTMLHelper should be like below,

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