1

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.