1

I enter data via a "create" view. To do this, I read various data from the existing database and use this, slightly modified, to fill out some fields in advance. This works with "int" values ​​without a problem, but I have problems with a date field. The date is not displayed in the create View field.

My controller looks like this.

var item = _context.Buchungen
  .OrderBy(p => p.Id)
  .LastOrDefault();

  if (item != null)
  {
     ViewData["Bankbeleg"] = item.Bankbeleg;
     ViewData["Belegnummer"] = item.Belegnummer + 1;
     ViewData["Datum"] = item.Datum.Value.ToShortDateString();
  }
return View("Create");

When I set a breakpoint, I can also see that the ViewData is occupied.

ViewData ["Datum"] = item.Datum.Value.ToString (); --> 26.08.2021 00:00:00
ViewData ["Datum"] = item.Datum.Value.ToShortDateString (); --> 26.08.2021

Unfortunately, both variants does not work

My Create View looks like this:

<div class="form-group">
    <label asp-for="Belegnummer" class="control-label"></label>
    <input type="text" asp-for="Belegnummer" class="form-control"
    value="@ViewData["Belegnummer"]" />
    <span asp-validation-for="Belegnummer" class="text-danger"></span>
</div>

<div class="form-group">
   <label asp-for="Datum" class="control-label"></label>
   <input type="date" asp-for="Datum" class="form-control" value="@ViewData["Datum"]" />
   <span asp-validation-for="Datum" class="text-danger"></span>
</div>

"Belegnummer" and other int values are shown correct, but not the "date" value.

Has anyone an idea why the date is not given or shown in the create view?

Paul_412
  • 47
  • 7
  • Does this answer your question? [How to set input type date's default value to today?](https://stackoverflow.com/questions/6982692/how-to-set-input-type-dates-default-value-to-today) – Heretic Monkey Aug 27 '21 at 23:47
  • 1
    While it seems from the title not to be the same, the answers there discuss the format the date value must have for an `input type="date"`, which is to say, "yyyy-MM-dd". – Heretic Monkey Aug 27 '21 at 23:48
  • Thank you for the hint: `ViewData["Datum"] = item.Datum.Value.ToString("yyyy-MM-dd");` is the solution – Paul_412 Aug 27 '21 at 23:58

1 Answers1

0

Try to use a Viewbag.Date = item.Datum.Value.ToString("yyyy/MM-dd");