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?