if I add Data annotations to my ViewModel the value of the DateTime field is always {1/1/0001 12:00:00 AM}.
ViewModel
public class EditReleaseViewModel : BaseViewModel
{
[Display(Name = "ReleaseDate", ResourceType = typeof(SharedResource))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy HH:mm}")]
[Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(SharedResource))]
public DateTime ReleaseDate { get; set; }
}
The controller is very straightforward, if I remove the data annotation and use the default everything works fine.
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(EditReleaseViewModel viewModel)
{
if (ModelState.IsValid)
{
...
}
}
View
<div class="form-group col-sm-12 col-md-6 col-lg-4">
<label asp-for="ReleaseDate" class="col-md-3 control-label"></label>
<div class="col-md-9">
<input asp-for="ReleaseDate" class="form-control" />
<span asp-validation-for="ReleaseDate" class="text-danger" />
</div>
</div>
It is an ASP.NET Core MVC RC2 application - perhaps I'm missing something there.
Change DateTime
to DateTime?
in your model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With