2016-06-22 28 views
5

nếu tôi thêm chú thích dữ liệu vào ViewModel, giá trị của trường DateTime luôn là {1/1/0001 12:00:00 AM}.Chú thích dữ liệu ngày giờ không hoạt động trong ứng dụng ASP.NET Core RC2

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; } 
} 

Bộ điều khiển rất đơn giản, nếu tôi loại bỏ các chú thích dữ liệu và sử dụng tất cả mọi thứ mặc định hoạt động tốt.

khiển

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<IActionResult> Edit(EditReleaseViewModel viewModel) 
     { 
      if (ModelState.IsValid) 
      { 
       ... 
      } 
     } 

Xem

<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> 

Nó là một ứng dụng ASP.NET MVC Lõi RC2 - có lẽ tôi là thiếu cái gì đó.

+0

bạn có cố sử dụng thuộc tính [DataType (DataType.Date)] không? – Rafael

Trả lời

1

Thay đổi DateTime thành DateTime? trong mô hình của bạn.

0

I figured này trên con đường của riêng tôi:

Thêm một dữ liệu chú thích [DataType (DataType.Date)], và sau đó 'xây dựng' dự án, và sau đó mọi thứ hoạt động.

Tôi không biết cách nào nhưng nó hoạt động cho tôi.

BTW, tôi đang làm theo hướng dẫn tại đây: https://docs.asp.net/en/latest/tutorials/first-mvc-app/adding-model.html

Các vấn đề liên quan