我在字段= 15-06-2014 中有默认日期,提交后我看到错误沟通:字段CreateDate必须是日期。 - 为什么?
我的ViewModel:
public class CommentViewModel
{
[Required(ErrorMessage = "CreateDate is required")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[DataType(DataType.Date, ErrorMessage = "Please enter a valid date")]
public DateTime CreateDate { get; set; }
public CommentViewModel()
{
CreateDate = DateTime.UtcNow;
}
}
我的行动:
public ActionResult Index()
{
CommentViewModel commentViewModel = new CommentViewModel();
return View(commentViewModel);
}
我的观点:
@model WebApplication5.Models.CommentViewModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.LabelFor(model => model.CreateDate, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.CreateDate)
@Html.ValidationMessageFor(model => model.CreateDate)
<input type="submit" value="Create" class="btn btn-default" />
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}