Save or remove accent on a Textareafor

Asked

Viewed 125 times

3

Hello.

I have a Textareafor which receives the HTML content, however when saving the data, the accents and special characters are encoded and I have problems later when editing or viewing the data.

Register view:

div class="row form-group">
    <div class="col-md-12 col-xs-12">
        @Html.LabelFor(model => model.Preparo, htmlAttributes: new { @class = "col-form-label" })
        @Html.TextAreaFor(model => model.Preparo, new  { @class = "form-control", @rows = 15, @value = "Digite o procedimento ou código, irá autocompletar"  })
        @Html.ValidationMessageFor(model => model.Preparo, "", new { @class = "text-danger" })
    </div>
</div>

Listing view:

  @Html.Raw(item.Preparo)

Model:

 [Required]
 [AllowHtml]
 public string Preparo { get; set; }

Visualização da parte do usuário

How could display the text with the accents for the user?

  • you have to place an html editor in an example: http://www.macoratti.net/17/03/mvc_rctbx1.htm

  • Hello @Virgilionovic, thanks for the comment, but this is not the problem, I already own an editor.

3 answers

0

As you are already using [Allowhtml] in the attribute check if you are using

<meta charset="UTF-8">

in the head of the page.

That could cause this problem. ps: check how the characters are coming into the controller

  • Thanks for the reply. In the controller arrives as it is in the preparation list, the head is correct.

  • had written <meta charset="UTF-8"> na in the text but disappeared

0

Consider checking the settings related to character globalization, see the solutions below:

1 - In your html, check if there is a tag related to character conversion, as in the example below:

<head>
  <meta charset="UTF-8">
</head>

If it does not exist within the tag head include.

The error persists?

2 - on your web.Config make sure to include the code:

<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="pt-br"/>

It directly affects the character conversion in the files.

Not yet?

3- Change the meta tag where it is utf-8 for ISO-8859-1.

Surely these auterações will solve your mistake.

If in last case, persist. As a final condition we can establish that the encoding of the files is difficult than expected, knowing this follow these steps by visual studio: ferramentas/opções/ambiente/documentos/ and change the option Salve documentos como Unicode quando os dados não puderem ser salvos.

  • Thanks for the reply, however this is not the case, otherwise all pages would be in trouble.

  • other pages have the queries?

  • Yeah, but not the way it is.

  • I’ve found the solution, thank you for your attention.

0


The error occurred for two reasons:

1º The generated html string was converted to uppercase, before reaching the controller.

2º I needed to use the method HttpUtility.HtmlDecode(), as follows.

Preparo = HttpUtility.HtmlDecode(preparoExame.Preparo);

You can access the documentation here: https://docs.microsoft.com/pt-br/dotnet/api/system.web.httputility.htmldecode?view=netframework-4.8

As it turned out:

Register view: note added to class property ignore-uppercase

<div class="row form-group">
    <div class="col-md-12 col-xs-12">
        @Html.LabelFor(model => model.Preparo, htmlAttributes: new { @class = "col-form-label" })
        @Html.TextAreaFor(model => model.Preparo, new  { @class = "form-control ignore-uppercase", @rows = 15, @value = "Digite o procedimento ou código, irá autocompletar"  })
        @Html.ValidationMessageFor(model => model.Preparo, "", new { @class = "text-danger" })
    </div>
</div>

Listing view:

<td data-title="Preparo">
     @Html.Raw(item.Preparo)
</td>

Listagem dos preparos com acentuação.

Controller: HttpUtility.HtmlDecode() no controller

Browser other questions tagged

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