0

I just started learning ASP.Net MVC, I'm trying to change my previous code (which was based on WebForm) to MVC but I encounter an Issue, Imagine I'm working on article to show a list of it:

Here is the Model:

public class Article
    {
        public int ID { get; set; }
        public string ArticleTitle { get; set; }
        public DateTime CreationDateTime { get; set; }
  }

Here is part of View:

<span> @Html.DisplayFor(modelItem => item.CreationDateTime )</span>

I wrote a method in controller to change Geregrian date to Persian date:

public string ConvertToPersianDate(object date)
        {
            clsWorkWithDate wwd = new clsWorkWithDate();
            date = wwd.gregorianToShamsi(Convert.ToDateTime(date));
            return date.ToString();
        }

Now I want to get access this function in view, something like this:

@ConvertToPersianDate(@Html.DisplayFor(modelItem => item.CreationDateTime ))

But the error message says that ConvertToPersianDate does not exist in the current context, it seems it's not accessible in view.

I had no problem when it was in WebForm list view:

<%#  ConvertToPersianDate(Eval("CreationDateTime")) %>

How can I resolve the matter?

Muhammad Musavi
  • 2,512
  • 2
  • 22
  • 35

1 Answers1

1

First, your function should be static.

public static string ConvertToPersianDate(object date)
{
    clsWorkWithDate wwd = new clsWorkWithDate();
    date = wwd.gregorianToShamsi(Convert.ToDateTime(date));
    return date.ToString();
}

Then you can use the function like this

@Html.DisplayFor(modelItem => YourAppName.Controllers.YourControllerName.ConvertToPersianDate(item.CreationDateTime) )

You can shorten this by adding an using at the top of the view.

@using YourAppName.Controllers
…
@Html.DisplayFor(modelItem => YourControllerName.ConvertToPersianDate(item.CreationDateTime) )

EDIT

This should work fine.

@YourAppName.Controllers.YourControllerName.ConvertToPersianDate(item.CreationDateTime)
Skye MacMaster
  • 894
  • 1
  • 10
  • 19
  • Thanks but I get an error message in run time: `Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions`. it has been explained [here](https://stackoverflow.com/questions/21754071/templates-can-be-used-only-with-field-access-property-access-single-dimension). – Muhammad Musavi Sep 23 '19 at 19:10
  • Oh, forgot that. Are you just displaying the date? You can just drop the DisplayFor then. I'll edit my answer. – Skye MacMaster Sep 23 '19 at 19:24
  • Thanks, it worked, that's exactly what I was looking for, anyway, is it the recommended approach? – Muhammad Musavi Sep 25 '19 at 16:49
  • I would probably create an extension method so that I could shorten it to @item.CreationDateTime.ToPersianDate() – Skye MacMaster Sep 25 '19 at 19:39
  • As for your answer, `item` is valid if it's in `@foreach (var item in Model)`, what if it's a single instance of model (not in `@foreach`), in that case VS says `item` does not exist. – Muhammad Musavi Sep 26 '19 at 14:28
  • I found the answer, it should be: `@YourAppName.Controllers.YourControllerName.ConvertToPersianDate(Model.CreationDateTime)` then. (Uppercase M in Model) – Muhammad Musavi Sep 26 '19 at 14:37