Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
Giúp mình sửa validate ngày tháng trong asp.net mvc
10:34 05-10-2017
4.878 lượt xem
3 bình luận
11:42 05-10-2017
model
[Required(ErrorMessage = "Yêu cầu nhập ngày")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public Nullable<System.DateTime> createDate { get; set; }
View:
<div class="col-sm-3">
<div class="form-group">
<div class="input-group date">
@Html.TextBoxFor(model =>model.createDate, new { htmlAttributes = new { @class = "form-control form-input datepicker" } })
@Html.ValidationMessageFor(model => model.createDate, "", new { @class = "text-danger" })
<span class="input-group-addon invisible"><i class="glyphicon glyphicon-th"></i></span>
</div>
</div>
</div>
Jquery
$('.input-group.date').datepicker({
format: 'dd/mm/yyyy'
});
khi nhập vào ngày
1/10/2017 thì ok nhưng nhập 28/10/2017 thì nó hiểu 28 là tháng nên báo lỗi The value '28/10/2017' is not valid for createDate.
có ai rành fix dùm mình với ạ
Mình sửa được rồi, đây là code
View:
Model:
khi dùng
thì trong view chỉ được dùng EditorFor / DisplayFor chứ không được dùng: TextBoxFor
và khi đã dùng DisplayFormat rồi thì trong view các bạn không cần dùng jquery để chạy datepicker nữa
bạn coi kỹ chỗ này