asp.net-mvcdisplaycyrillicdisplayattribute

Display attribute in MVC doesnt display cyrillic in the HTML


I try to render cyrillic word "име" instead of the name of the column(FirstName) in the table from the DataBase but instead i receive some strange symbols "Èìå". I have a <globalization fileEncoding="utf-8" /> in my webconfig and also I've checked the files containing the code and they are all saved as UTF-8.

I am pretty sure the problem is caused by something in ModelRS.edmx or the files contained in this hierarchy. Here are two screenshots that provide more of the code.

enter image description here

enter image description here

Should I just hardcode "Име" in the HTML as I tried and works fine but it feels like bad practice and this is a project I'm showing to a exam judges?

My html block (see also the screenshot for comperhensive info):

@using PagedList.Mvc;
@model PagedList.IPagedList<Rating_System.Models.tblTeacher>

@Html.DisplayNameFor(model => model.First().FirstName)  
// I have .First() because the web page has pagination 

My controller:

public class TeachersController : Controller
    {
        private RSEntities db = new RSEntities();

        // GET: Teachers
        public ActionResult Index(int page = 1, int pageSize = 8)
        {
            List<tblTeacher> listTeachers = db.tblTeachers.ToList();
            PagedList<tblTeacher> model = new PagedList<tblTeacher>(listTeachers, page, pageSize);
            //return View(db.tblTeachers.ToList()); 
            return View(model);
        }
    }

My "Model":

 public partial class tblTeacher
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public tblTeacher()
    {
        this.tblRatings = new HashSet<tblRating>();
        this.tblTeachersDisciplines = new HashSet<tblTeachersDiscipline>();
    }

    public int ID { get; set; }

    [Required]
    [Display(Name = "Име:")]
    [StringLength(50)]
    public string FirstName { get; set; }
}

Solution

  • AFAIK, Visual Studio has option to save a file with different encoding. Open your CS file which contains model class, select File => Save As => Save with Encoding and choose "UTF-8 with signature" (or other encoding you've needed) on Advanced Save Options.

    If solutions above still doesn't work, open your Layout.cshtml file which contains base HTML tag for view pages, then use both lang attribute and meta tag:

    <html lang="ru">
    
    <meta charset="UTF-8">
    

    This will declare entire view page containing the layout page as Russian with UTF-8 encoding, thus Cyrillic characters should rendered properly as given by Display attribute.

    For Intellisense problem that doesn't recognize your model class name, go to Tools => Options => Text Editor => C# => Intellisense, and check if 2 conditions below applied:

    1. "Show completion list after a character is typed" has checked.

    2. "Commited by typing the following characters" text box contains {}[]().,:;+-*/%&|^!~=<>?@#'"\.

    Other suggestions welcome.