This is my code:
Model:
[Required]
[DataType(DataType.Text)]
[Display(Name = "Your company's name")]
public string CompanyName { get; set; }
View:
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", @placeholder = @Html.DisplayNameFor(m => m.CompanyName), @id = "companyname" })
It will be rendered like this:
Your company's name
html output:
<input class="account-input" data-val="true" data-val-required="The Your company's name field is required." id="companyname" name="CompanyName" placeholder="Your company&#39;s name" type="text" value="">
It should be look like this:
Your company's name
Why is the text does not render correctly and how can I prevent this?
I already tried this:
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", @placeholder = @Html.Raw(@Html.DisplayNameFor(m => m.CompanyName)), @id = "companyname" })
and this
@Html.TextBoxFor(m => m.CompanyName, new { @class = "account-input", @placeholder = @Html.Encode(@Html.DisplayNameFor(m => m.CompanyName)), @id = "companyname" })
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:
There is also
HttpUtility.HtmlDecode
, which might help with our save action.update
if you wrap
HttpUtility.HtmlDecode
around your place holder:the placeholder returns as: placeholder="Your company's name"
Wouldn't it be easier to just skip using Html.DisplayNameFor? Decoding and re-encoding the text seems unneeded.
Here's an helper displayplaceholderfor that will present placeholder without encoding.
http://www.davidberube.me/displayplaceholderfor-mvc-placeholder-for-special-characters/
-- EDIT --