ASP MVC Razor encode special characters in input placeholder

I think this post will help you:

HTML encode decode c# MVC4

I think there are other ways to get this behaviour, but this is one option of using the TextBox:

@Html.TextBox("CompanyName", HttpUtility.HtmlEncode("Your company's name"))

There is also HttpUtility.HtmlDecode, which might help with our save action.

update

if you wrap HttpUtility.HtmlDecode around your place holder:

@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", 
@placeholder = HttpUtility.HtmlDecode(Html.DisplayNameFor(x => x.CompanyName).ToHtmlString()), 
@id = "companyname" })

the placeholder returns as:
placeholder=”Your company’s name”

Leave a Comment