----我的控制器---
public ActionResult Edit(int id)
{
var dbContext = new MyDBDataContext();
var userDetails = dbContext.Users.FirstOrDefault(userId => userId.UserId == id);
var user = new Management.Models.User();
if (userDetails != null)
{
user.DOB = Convert.ToDateTime(userDetails.DOB);
user.POS = Convert.ToDateTime( userDetails.POS);
}
return View(user);
}
----- ------模型
[Required(ErrorMessage = "Please Enter the Date that Start service")]
[Display(Name = "Period Of Service")]
//[DisplayFormat(DataFormatString = "{dd-mm-yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime POS { get; set; }
[Required(ErrorMessage = "Please Select the Date of birth")]
[Display(Name = "Date of birth")]
//[DisplayFormat(DataFormatString = "{dd-mm-yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime DOB { get; set; }
---------查看-------
<div class="editor-label">
@Html.DisplayName("Date Of Join")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.POS)
@Html.ValidationMessageFor(model => model.POS)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Salary)
</div>
答案 0 :(得分:0)
如果您使用EditorFor
生成浏览器datepicker(HTML5),那么(请注意格式字符串)
Required(ErrorMessage = "Please Enter the Date that Start service")]
[Display(Name = "Period Of Service")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime POS { get; set; }
POS
的值将显示在浏览器的文化中。