0

I am going to convert Georgian date time to Persian in display views. I used a function to this in view body, a method in core class, neither worked. How to use a function to convert the date?

@Html.DisplayFor(modelItem => item.DateRegistered)
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Amir Parsi
  • 89
  • 8

1 Answers1

0

I solved the problem. I have added a [NotMapped] property to the class like this:

    [Display(Name = "تاریخ ثبت")]
    [NotMapped]
    public string PersianDate => GetPersianDate();
    public string GetPersianDate()
    {
        System.Globalization.PersianCalendar persianCalendar = new System.Globalization.PersianCalendar();
        return string.Join("/", new string[] {
            persianCalendar.GetYear(DateRegistered).ToString(),
            persianCalendar.GetMonth(DateRegistered).ToString(),
            persianCalendar.GetDayOfMonth(DateRegistered).ToString()});
    }

Then I used modelItem.PersianDate in view like this:

@Html.DisplayFor(model => model.PersianDate)

and I think is's a suitable way to use any other methods to convert other properties as well.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Amir Parsi
  • 89
  • 8