user1073075 user1073075 - 3 months ago 16
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

<span>
@Model.ToString("dd / MM / yyyy")
</span>


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:

[UIHint("MyDate")]
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

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); } }