jQuery UI的日期选择器和MVC视图模型datetime类型 [英] jquery ui datepicker and mvc view model type datetime
问题描述
我使用我的视图模型jQuery的日期选择器
I am using jquery datepicker on my view model
下面是我的看法:
@Html.TextBoxFor(o => o.JobStartDate, new { id = "dt1", @class = "input-block-level" })
@Html.ValidationMessage("JobStartDate")
和我的脚本:
$("#dt1").datepicker({ dateFormat: "dd/mm/yy" });
如果我的日期为&lt一切工作正常;现场开始日期必须是日期= 12,如果我的日期是超过12,它会告诉我一个验证错误消息说(我使用jQuery验证)
Everything works fine if my date is <= 12, if my date is over 12, it will show me an validation error message saying "The field Start Date must be a date." (I am using jquery validation)
例如:日期16/12/2014会给我的错误,而2014年12月12日不会
For example: date 16/12/2014 will give me the error while 12/12/2014 won't
下面是我的视图模型:
[Required]
[DataType(DataType.Date)]
[Display(Name = "Start Date")]
public DateTime JobStartDate { get; set; }
我怀疑我的视图模型在格式毫米,而在我的日期选择我指定的DD / MM / YY预期日期/ DD / YYYY,有没有办法告诉我的视图模型,我期待DD / MM / YY格式,因此,如果日期是它不会引发错误消息> = 12。
I am suspecting that my view model is anticipating a date in the format mm/dd/yyyy while on my datepicker i specified dd/mm/yy, is there a way to tell my viewmodel that I am expecting dd/mm/yy format so that it doesn't throw an error message if date is >= 12.
推荐答案
您可以看一下使用全球化时代的jQuery 一个>或以下内容添加到您的脚本(假设当然,服务器文化日期格式为DD / MM / YYY的)
You can look at using the jquery globalize or add the following to your script (assuming of course that the server culture date format is 'dd/MM/yyy')
$.validator.addMethod('date', function (value, element) {
if (this.optional(element)) {
return true;
}
var valid = true;
try {
$.datepicker.parseDate('dd/mm/yy', value);
}
catch (err) {
valid = false;
}
return valid;
});
$('#dt1').datepicker({ dateFormat: 'dd/mm/yy' });
和请用 @ Html.ValidationMessageFor(M = GT; m.JobStartDate)
这篇关于jQuery UI的日期选择器和MVC视图模型datetime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!