0

when I use this code in my view:

@Html.EditorFor(model => model.DateAdded)

I get the date formatted like this:

14-10-2012 11:44:34

Now I want to show only the date, and in this format.

2012-10-14

So I thought I'd use a EditorTemplate

But in a EditorTemplate I start from scratch / I start with no HTML, and I have to eventually render this (which is what is rendered by .Net when i use this code@Html.EditorFor(model => model.DateAdded) :

<input class="text-box single-line" data-val="true" data-val-date="The field DateAdded must be a date." data-val-required="The DateAdded field is required." id="DateAdded" name="DateAdded" type="datetime" value="14-10-2012 11:44:34" />

Can I somehow prevent myself from duplicating all these code (<input class="text-box single-line" data-val="true" data-val-date="The field DateAdded must be a date." data-val-required="The DateAdded field is required." id="DateAdded" name="DateAdded" type="datetime") because the framework might change it in the future, and then my template is stuck with that (old) code.

So what I want to say in my editor template is this: do everything you'd normally do, except format the value of the DateTime input differently.

Is that possible?

Typing this I realize I've made the assumption that for me to change the format of a DateTime in a @Html.EditorFor method, I've to create a EditorTemplate. Is that assumption correct?

Michel
  • 23,085
  • 46
  • 152
  • 242

1 Answers1

2

So I thought I'd use a EditorTemplate

How about decorating your view model property with the using the [DisplayFormat] attribute:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime DateAdded { get; set; }

Typing this I realize I've made the assumption that for me to change the format of a DateTime in a @Html.EditorFor method, I've to create a EditorTemplate. Is that assumption correct?

Yes, that's one way of changing the format. Another way is to use the DisplayFormat attribute.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks, that indeed does work. I was hoping that the `ApplyFormatInEditMode` would also do something for the validation, but i guess i have to tweak the validationscripts to let this date be seen as 'valid'? – Michel Apr 15 '13 at 12:07
  • No, this has nothing to do with validation. You could use a custom model binder for this task. I have illustrated such binder here: http://stackoverflow.com/a/7836093/29407 – Darin Dimitrov Apr 15 '13 at 12:12
  • I thought the modelbinder was only for the convert of the entered data in the textbox to the .Net object, does it also kick in for the client side validation (ie before the postback to the server occurs?) – Michel Apr 16 '13 at 07:56
  • No, the model binder doesn't kick in for client side validation. Client side validation (as its name suggests) runs on the client and is pure javascript. The model binder is something that runs on the server. – Darin Dimitrov Apr 16 '13 at 08:12