[DevTip] ASP.NET MVC shows placeholder with ‘broken’ font


ISSUE:

Model:

[Required][DataType(DataType.Text)][Display(Name="Your company's name")]publicstringCompanyName{ 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:

input placeholder text

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&amp;#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?

SOLUTION

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”.

Note: remembe to using System.Web namespace

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s