user1073075 user1073075 - 1 year ago 95
ASP.NET (C#) Question

Display template not displaying with UIHint attribute

I'm trying out display templates in MVC 4.

I create my displaytemplate in Views\Shared\DisplayTemplates\MyDate.cshtml

@model System.DateTime

@Model.ToString("dd / MM / yyyy")

I try to use it in my view:

@Html.DisplayFor(Model => Model.TestDate, "MyDate")

So far so good. Now I want to do the same thing with the UIHint attribute. So I add another field in my model:

public DateTime Yesterday = DateTime.Now.AddDays(-1);

So I figure I can simply add it to my view like this:

@Html.DisplayFor(Model => Model.Yesterday)

But oh no, my markup from my displaytemplate MyDate.cshtml is nowhere to be seen... What am I doing wrong??

Answer Source

Your problem is that you using field, but not property.

That's what you should do:

public DateTime Yesterday { get { return DateTime.Now.AddDays(-1); } }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download