views:

28

answers:

1

I'm sure I'm missing something pretty simple here. I've created a custom DateTime display template. This works fine when using:

<%= Html.DisplayFor(x=>x.DateTimeProperty) %>

However, I have a situation where I am iterating over objects in the Model in a for loop while inside a partial control. I want a DateTime property to use the display template, but I can't figure out how to instruct it to do so. The property is a DateTime as it should be. The looks like:

 <% foreach (var item in Model) { %>
       <%= Html.Encode(item.DateTimeProperty)  %>
 <% } %>

I can't call Html.DisplayFor(...) because of this structure, but I want all DateTime properties to use my DateTime display template.

I've tried adding a UIHint via DataAnotations, but that doesn't seem to have worked.

How do I do this?

A: 

Not that this question got many views, but I found the answer and figured I'd post in case someone was curious.

The solution was rather simple:

<%= Html.DisplayFor(x=>item.ReviewDate, "DateTime") %>

My custom Display template (named: DateTime) is defined as:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime?>" %>
<% 
    string value = (Model.HasValue ? Model.Value.ToShortDateString() : ""); 
%>
<%= Html.Encode(value) %>

I guess I didn't realize that when you declared the lambda, you didn't have to use the variable on the left side of the '=>' at all. I always thought it was mandatory.

Jason