DateTime data annotations do not work in ASP.NET Core RC2 application

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 simple, if I delete 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> 

This is an ASP.NET Core MVC RC2 application - maybe I missed something there.

+5
source share
2 answers

Change DateTime to DateTime? in his model.

+1
source

I understood this in my own way:

Add another data annotation [DataType (DataType.Date)], and then “Build” the project, and then everything will work.

I don’t know, but it works for me.

By the way, I follow the tutorial here: https://docs.asp.net/en/latest/tutorials/first-mvc-app/adding-model.html

0
source

All Articles