Question

I have some code like this in a cshtml view

@{  int i = 0;
    foreach (var record in Model.Records)
    {
    <tr>
        <td>@record.CreatedDate</td>
        <td>@Html.DisplayFor(x => x.Records[i].CreatedDate)</td>
    </tr>
        i++;
    }
}

and in my ~/Views/Display folder I have a file called DateTime.cshtml with this in

<p>@ViewData.Model.ToShortDateString()</p>

so my view is rendering two version of the CreatedDate, one with the full DateTime string and one with the short date string.

Can I configure my ASP.NET MVC4 project to use the display template I have created whenever it displays a DateTime, without calling DisplayFor explicitly? ie how can the line '@record.CreatedDate' use my DateTime template?

Was it helpful?

Solution

No you can't. there is no way to configure MVC to use a template with this code:

'@record.CreatedDate'

You will have to keep using DisplayFor which isn't too much to write by the way...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top