ASP MVC Razor encode special characters in input placeholder
Asked Answered
T

3

27

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&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?

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" })
Telmatelo answered 6/3, 2013 at 11:42 Comment(2)
your placeholder is encoding as: Your Company&amp;#39;s name?Halophyte
Yes, exactly as described above.Telmatelo
H
51

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&#39;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"

Halophyte answered 6/3, 2013 at 12:49 Comment(3)
The link you post doesn't help. I already tried @Html.Raw. For the placeholder property it does not work.Telmatelo
Do you want to encode the entity to become the ' in the textbox?Halophyte
Similarly, you can use HtmlStringColombia
D
4

Here's an helper displayplaceholderfor that will present placeholder without encoding.

http://www.davidberube.me/displayplaceholderfor-mvc-placeholder-for-special-characters/

-- EDIT --

public static partial class Extensions
{
   public static MvcHtmlString DisplayPlaceHolderFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
   {
      var result = html.DisplayNameFor(expression).ToHtmlString();
      return new MvcHtmlString(System.Web.HttpUtility.HtmlDecode(result.ToString()));
   }
}
Decolorant answered 28/4, 2015 at 19:37 Comment(0)
I
1

Wouldn't it be easier to just skip using Html.DisplayNameFor? Decoding and re-encoding the text seems unneeded.

public static MvcHtmlString DisplayPlaceHolderFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    var metaData = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    var resolvedDisplayName = metaData.DisplayName ?? metaData.PropertyName;

    if (!string.IsNullOrEmpty(resolvedDisplayName))
        return new MvcHtmlString(resolvedDisplayName);

    var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
    resolvedDisplayName = htmlFieldName.Split('.').Last();

    return new MvcHtmlString(resolvedDisplayName);
}
Intermolecular answered 11/2, 2016 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.