Camilo Terevinto
Camilo Terevinto

Reputation: 32088

ViewComponent/PartialView get wrongly encoded and does not use UTF8

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

Upvotes: 3

Views: 1059

Answers (2)

Fl&#225;vio Torres
Fl&#225;vio Torres

Reputation: 31

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.

Upvotes: 3

Camilo Terevinto
Camilo Terevinto

Reputation: 32088

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

Upvotes: 1

Related Questions