views:

24

answers:

1

Following on from an earlier question, I'm having problems using an Editor template for datetime fields, below is the code for the editor template (called "EditDateTime").

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>

<%= Html.TextBox("", (Model.HasValue ? Model.Value.Date.ToString("dd/MMM/yyyy") : 
DateTime.Today.ToString("dd/MMM/yyyy")), new { @class = "date" })%>

Here is the call to use the editor template,

<%=Html.EditorFor(Model => Model.StartDate, "EditDateTime") %>

All works ok, except the formatting is ignored. The Asp.Net MVC2 Framework book documents templates and specifically states

We’re passing an empty string for the name parameter because the framework will automatically prefix this with the field name corresponding to the model item being rendered;

So I shouldnt need an id, so why is the formatting being ignored? HOWEVER - if I include an ID, the formatting is adhered to, BUT the model binding is then broken.

Help?


Just to clarify.. If I give the HMTL.Textbox an ID I get the following... The date format is correct but I lose the model binding as the ID is changed from "StartDate" to "StartDate_xx"

<input class="date" id="StartDate_xx" name="StartDate.xx" type="text" value="02/May/2012" />

And without the Id, it looks like this... model binding is correct but format is not.

<input class="date" id="StartDate" name="StartDate" type="text" value="05/02/2012 00:00:00" />
A: 

try using

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

works for me.

everything looks fine really... is the item you are sending a Datetime or a Datetime?

Stefanvds
Its a nullable Datetime?
TChamberlainGE
This doesnt work - it has to be an editor template not a display template. Thank you very much for you help though.
TChamberlainGE