0

I have a model with two dates, I pass both in as Proposed "08/07/2013 08:00:00" and CurrentFocusDate "08/07/2013 00:00:00" however somewhere something is going wrong as in the page they are rendered differently (see output below) Anyone have an idea why two alsmot identical properties would get rendered differently?

Model

public AdminNoteViewModel
{
    [HiddenInput(DisplayValue = false)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime ProposedDateTime { get; set; }

    [HiddenInput(DisplayValue = true)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime CurrentFocusDate { get; set; }

    [HiddenInput(DisplayValue = true)]
    public string NavigationLink { get; set; }
}

View

@Html.EditorFor(model => model.ProposedDateTime)
@Html.ValidationMessageFor(model => model.ProposedDateTime)

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

@Html.HiddenFor(model => model.NavigationLink)

Controller

 public ActionResult AddNote(DateTime proposedEventDateTime, long productId, DateTime currentFocusDate, string navigationLink)
 { 
     var model = new AdminNoteViewModel
     {
         ProductId = productId,
         ProposedDateTime = proposedEventDateTime,
         CurrentFocusDate = currentFocusDate,
         NavigationLink = navigationLink
     };

     return View(model);
 }

This is the source rendered

<input data-val="true" data-val-date="The field ProposedDateTime must be a date." data-val-required="The ProposedDateTime field is required." id="ProposedDateTime" name="ProposedDateTime" type="hidden" value="07/08/2013 08:00:00" />
<span class="field-validation-valid" data-valmsg-for="ProposedDateTime" data-valmsg-replace="true"></span>

<input data-val="true" data-val-date="The field CurrentFocusDate must be a date." data-val-required="The CurrentFocusDate field is required." id="CurrentFocusDate" name="CurrentFocusDate" type="hidden" value="08/07/2013 00:00:00" />

<input id="NavigationLink" name="NavigationLink" type="hidden" value="Civica.DrugAdmin.UI.Models.AdminNoteViewModel" />

When I debug, the model osted to the view has both dates correctly formatted, however when they render on the page one of the them (currentFocusDate) gets switched around.

Cookie
  • 594
  • 1
  • 12
  • 34
  • Out of curiosity, what if you remove the `HiddenInput` attribute from the model? I've never used this attribute personally and have just specified either `EditorFor` or `HiddenFor` depending on whether I wanted to show the value or not. – asymptoticFault Aug 06 '13 at 15:11
  • I makes no difference whether the decoration is there or not. It actually means that editor for renders an input type hidden. – Cookie Aug 07 '13 at 07:18

1 Answers1

3

By design the HiddenFor helper (which eventually gets used by the EditorFor) always uses the current culture format when rendering its value. If you want to override this behavior you could write a custom editor template (~/Views/Shared/EditorTemplates/HiddenInput.cshtml):

@if (!ViewData.ModelMetadata.HideSurroundingHtml) 
{
    @ViewData.TemplateInfo.FormattedModelValue
}
@Html.Hidden("", ViewData.TemplateInfo.FormattedModelValue)

and then you will be able to specify the format using the DisplayFormat attribute and override the current culture's format:

[HiddenInput(DisplayValue = false)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime ProposedDateTime { get; set; }
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • It doesn't seem to be that. If I change them both to use editor for I still get the same issue, that one is in US format the other UK – Cookie Aug 07 '13 at 07:21
  • Make sure you have placed and named your template correctly: `~/Views/Shared/EditorTemplates/HiddenInput.cshtml`. Also did you decorate your Date properties on the view model with the `[DisplayFormat]` attribute as shown in my answer? Verify that the custom editor template is actually being used by placing some easy to recognize text in it. – Darin Dimitrov Aug 07 '13 at 07:25
  • I changed it to editor for rather than hidden input, and renamed the field and that seemed to fix it. – Cookie Aug 07 '13 at 08:04