Reputation: 3571
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??
Upvotes: 1
Views: 798
Reputation: 12491
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); } }
Upvotes: 1