2

My ViewComponent content is getting encoded for some reason, and I have not been able to find a way to render it properly.

In my view:

<vc:my-component></vc:my-component>

My component:

@model List<MyViewModel>

@foreach (var myViewModel in Model)
{
    <table class="table table-bordered">
        <thead class="thead-default">
            <tr>
                ...
                <th>Miércoles</th>
                ...
            </tr>
        </thead>
        <tbody class="tbody-default">
            ...
        </tbody>
    </table>

}

The HTML gets rendered as:

<th>Mi�rcoles</th>

I have tried using this, but didn't make any difference:

<th>@Html.Raw("Miércoles")</th>

How can I make ASP.NET Core render the ViewComponent without encoding it?

EDIT:
The same happens with PartialViews

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • Do you have in your _Layout.cshtml? Open your component in Notepad++. Maybe the file isn't being saved right. I just tried putting Miércoles in my viewcomponent and it worked. – Paul Totzke May 22 '17 at 04:42
  • @PaulTotzke Notepad (don't have ++ here) shows the é is correctly saved, and I do have that line (first one right below ). This do works on Views and PartialViews, though, only the ViewComponents (two of them) are giving the problem – Camilo Terevinto May 22 '17 at 10:41
  • Can you add this view component with a PartialView where this is working on the same page? Is this vc being sent via ajax? Copy the text from a working page to your view component? – Paul Totzke May 22 '17 at 15:20
  • @PaulTotzke No, the component is rendered as a TagHelper or as a Component (server-side, no ajax). I will try tonight using a PartialView instead and let you know – Camilo Terevinto May 22 '17 at 15:56
  • @PaulTotzke well, this is weird, the same happens for PartialViews – Camilo Terevinto May 22 '17 at 22:55
  • @PaulTotzke however, if I paste the contents onto the main page, it works just fine – Camilo Terevinto May 22 '17 at 22:57
  • Can you throw an example up on github or somewhere? – Paul Totzke May 23 '17 at 15:30
  • @PaulTotzke you may want to see this thread: https://github.com/aspnet/Razor/issues/1375 – Camilo Terevinto May 23 '17 at 15:31

2 Answers2

3

I had the same problem. Even placing the encode on the Layout Page, Partial View did not work.

To solve this, I had to change the file's encode as follows:

1 - Broken page

enter image description here

2 - Open the folder where the file is located

enter image description here

3 - Open the file that has the characters that break, in a notepad

enter image description here

4 - Click in (File -> Save As)

enter image description here

5 - Change encode to UTF8 and Save the file

enter image description here

6 - Check the result

enter image description here

See.. this solution also works with JavaScript.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • If you have the time, you can edit your answer such that instead of showing URL, you can display the image directly using `[![enter image description here][1]][1]` with `[1]: https://i.stack.imgur.com/Fwpmd.png` – HardcoreGamer Aug 20 '21 at 01:43
  • @HardcoreGamer I did it for Flávio. New users can only link to images until they have enough reputation, and I don't expect anyone to do all that markdown syntax when for someone with enough reputation is just a click :) – Camilo Terevinto Aug 20 '21 at 08:12
  • @CamiloTerevinto Didn't know until now, thanks for your help and contribution. – HardcoreGamer Aug 20 '21 at 08:27
1

This is the most stupid bug ever.

Since for new projects everything goes just fine, I decided I'd try to create a new project and copy my files. Guess what? Same problem arose. After childishly copying the text to one of the working projects, saving the file and copying the saved file to the project with problems, it worked just fine.

I find it completely weird that VS, Notepad and Notepad++ can open the file just fine, but ASP.NET Core can't.

So, solution? Copy the data over and replace the files...

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • My guess is that the old files are encoded differently. Open both files in plain old notepad and go to'Save As' to check how each are being encoded. https://stackoverflow.com/questions/3710374/get-encoding-of-a-file-in-windows – Paul Totzke May 24 '17 at 20:22
  • @PaulTotzke They are literally the same, and they are not "old" files, they were created at the same time as most of my views/project (last weekend!). – Camilo Terevinto May 24 '17 at 20:35