0

I have a property that I need to display on an editor

[Display(Name = "Created on")]
[UIHint("DateDisabled")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime? CreationDate { get; set; }

The template editor looks like this

@model DateTime?

@{
   var culture = ...// retrieve culture;
}

@Html.TextBoxFor(m => m, "{0:"+ culture.DateTimeFormat.ShortDatePattern+"}",
   new { @class = "form-control", style = "width:100%", disabled = "disabled" })

And in a view I am using something like this

@Html.HiddenFor(m => m.CreationDate)
@Html.EditorFor(m => m.CreationDate)

The form contains other fields. When the editor is opened the CreationDate looks OK

<input name="CreationDate" id="CreationDate" type="hidden" value="10/12/2018 11:14:10 AM">
<input name="CreationDate" disabled="disabled" class="form-control" id="CreationDate" style="width: 100%;" type="text" value="10/12/2018">

But if the form contains some wrong value and the server posts back

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Update(Model viewModel)
{
    if (!ModelState.IsValid)
    {
        return View("Edit", Model);
    }
    ...
}

the creation date ignores the format and the generated html look like this

<input name="CreationDate" id="CreationDate" type="hidden" value="10/12/2018 11:14:10 AM">
<input name="CreationDate" disabled="disabled" class="form-control" id="CreationDate" style="width: 100%;" type="text" value="10/12/2018 11:14:10 AM">

As you can see the format was ignored and the full date and time is displayed.

Is this function as design? What can I do to influence the display of the date after post with errors?

Dan
  • 683
  • 2
  • 8
  • 24
  • Why do you have a hidden input and a disabled textbox for the same property? And the reason is that `HtmlHelper` methods use the values from `ModelState`, not the model property if they exist, and a `ModelState` value has been added in your POST method (from the hidden input) –  Oct 16 '18 at 09:48
  • I need the hidden because the input is disabled and I lose the value otherwise. So you are saying that is function as design? – Dan Oct 16 '18 at 13:43
  • Refer [TextBoxFor displaying initial value, not the value updated from code](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) to explain the behavior. And why not just use a readonly textbox rather that a disabled textbox plus the hidden input(which is also generating invalid html because of the duplicate `id` attributes)? –  Oct 17 '18 at 09:28
  • And why do you have a `[DisplayFormat]` with `ApplyFormatInEditMode` when you do not use it? –  Oct 17 '18 at 09:29
  • And you could have just used `@Html.TextBoxFor(m => m, "{0:d"}", new { .. })` –  Oct 17 '18 at 09:31

1 Answers1

0

For your issue, I suggest you try options below:

  1. Change the model like:

    [Display(Name = "Created on")]
    [UIHint("DateDisabled")]
    [DataType(DataType.Date)]
    //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:dd\/MM\/yyyy}")]
    public DateTime? CreationDate { get; set; }
    
  2. Change the View like:

    @Html.TextBoxFor(model => model.CreationDate, @"{0:yyyy\/MM\/dd}", new { @class = "datepicker" })
    
Edward
  • 28,296
  • 11
  • 76
  • 121
  • It did not work. The time appeared again after post. – Dan Oct 16 '18 at 07:15
  • @Dan, is there any reproducable project? – Edward Oct 16 '18 at 07:23
  • unfortunately no because the project I have is very big – Dan Oct 16 '18 at 13:44
  • @Dan Share us how to reproduce your issue with [MVCModels/Edit/5](https://github.com/Edward-Zhou/AspNetCore/blob/5ccd2c437f6e32dce5e3361272fa38868978d7f0/EFCorePro/Controllers/MVCModelsController.cs#L89) – Edward Oct 16 '18 at 14:02