C# returns an HTML string

Asked

Viewed 408 times

0

Good,

I use the following code to convert a numeric value according to the user culture:

value="@(Model.KnowAcquisition.Cost.HasValue ? Model.KnowAcquisition.Cost.Value.ToString("n2", CultureInfo.CurrentCulture).ToString() : string.Empty)" />

However, C# turns into this:

<input type="text" class="famo-input famo-text-10" name="cost" value="1&nbsp;000,25">

My problem is that it should be a space instead of the &nbsp. I have tested with normal strings and shows the space, only when using Tostring() with an associated culture does it show the &nbsp.

How can I change?

2 answers

1

You can use the Html.Raw() so that there is no HTML encoding in the string.

Would look like this:

<input type="text" class="famo-input famo-text-10" name="cost" value="@Html.Raw(Model.KnowAcquisition.Cost.HasValue ? Model.KnowAcquisition.Cost.Value.ToString("n2", CultureInfo.CurrentCulture).ToString() : string.Empty)" />

Also try as follows (using Httputility.Htmldecode()), if the string is already encoded for HTML:

<input type="text" class="famo-input famo-text-10" name="cost" value="@Html.Raw(Model.KnowAcquisition.Cost.HasValue ? HttpUtility.HtmlDecode(Model.KnowAcquisition.Cost.Value.ToString("n2", CultureInfo.CurrentCulture).ToString()) : string.Empty)" />
  • I haven’t tried it yet...but there is some explanation why it works with some strings and not with others?

  • There is. They are special characters in HTML, which have a different Encounter. That’s why they are replaced by other equivalents. Here are some examples: http://erikasarti.net/html/accent/

  • I’m sorry but I didn’t notice, if I put in HTML the string "hello world", space appears. This string has a space and appears with a special character...that’s what I don’t understand.

  • One thing, the solution doesn’t work. The &nbsp.

  • I will update the response with a possible solution, as the HTML Encounter continues to occur

  • It still doesn’t work.... I’ve tried formatting dates and spaces appear...

  • Good. unfortunately I can not help you anymore, in which case already escaped my field of knowledge, I suggest checking the charset of your HTML document and check if apply the crop in .ToString() influence the view rendering somehow more.

Show 2 more comments

1


I already found a solution, I had to replace the C#:

@{ 
    string cost = (Model.KnowAcquisition.Cost.HasValue ? Model.KnowAcquisition.Cost.Value.ToString("n2", CultureInfo.CurrentCulture) : string.Empty).Replace("\u00A0", " ");
}

<input type="text" class="famo-input famo-text-10" name="cost" value="@cost" />

Apparently, Numberformatinfo.Numbergroupseparator equals Non-breaking Space.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.