views:

79

answers:

4

Code:

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

display this: 8/24/2009 12:00:00 AM but I need only date (8/24/2009). It is possible to do without any formating in controller action

A: 

I would say that formatting is a responsibility of the view, not the controller, so format the output like this:

<% foreach (var item in Model) { %>    
<td> 
    <%= Html.Encode(item.BirthDate.ToShortDateString() %>
</td> 
<% } %> 
Daniel Renshaw
I have this error with your code: 'System.Nullable<System.DateTime>' does not contain a definition for 'ToShortDateString' and no extension method 'ToShortDateString' accepting a first argument of type 'System.Nullable<System.DateTime>' could be found (are you missing a using directive or an assembly reference?)
Ognjen
This line of code is work:<%= Html.Encode(item.BirthDate.Value.ToShortDateString() %>you forget `Value`
Ognjen
That line of code will not work if the nullable date time does *not* have a value. Calling the Value property will throw an exception in that case. See my answer below for the syntax you need.
Steve Michelotti
A: 

If item.BirthDate is type of DateTime you can use ToShortDateString() method:

item.BirthDate.ToShortDateString();
Misha N.
+5  A: 

There are a couple of ways to go about it. If you're using MVC 2, you could use a DisplayTemplate. Just put a DateTime.ascx file in the folder called /Views/Shared/DisplayTemplates and the line of code it would have in it is:

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

(Note: if you use the <%: syntax then you don't need the Html.Encode() because <%: does the HTML encoding for you - but that's only if you're using VS2010. If you're using MVC 2 with VS2008, then stick with the Html.Encode() for this part) Then in your view you would simply do this:

<%: Html.DisplayFor(m => m.BirthDate) %>

That will change the format to only have the Date for all DateTime's in your application. Of course you could put that directly in the view as well. But the DisplayTemplate will change it for all other DateTime's as well and you won't have to concern yourself with it in the view since it happens for your automatically.

Steve Michelotti
I'm using MVC2 and VS 2008
Ognjen
See my comment @Daniel Renshaw
Ognjen
Ah, you didn't mention it was nullable. In that case, in your DisplayTemplate, put this:<%: item.HasValue ? Model.Value.ToShortDateString() : string.Empty %>
Steve Michelotti
Are you sure that my view have only `Html.DisplayFor(m => m.BirthDate)`
Ognjen
Yes. What behavior are you seeing? And did you put the DateTime.ascx in the DisplayTemplates folder? @Control directive look exactly like mine above?
Steve Michelotti
Ok, now all work fine, tnx
Ognjen
A: 
  <% foreach (var item in Model) { %>   
  <td>
     <%= Html.Encode(item.BirthDate.ToString("MM/dd/yyyy")) %>
  </td>
  <% } %>
Error: No overload for method 'ToString' takes '1' arguments
Ognjen
Yes. You are using a Nullable DateTime. O.o