jquery ui datepicker和mvc视图模型类型datetime [英] jquery ui datepicker and mvc view model type datetime

查看:140
本文介绍了jquery ui datepicker和mvc视图模型类型datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的视图模型中使用jquery datepicker

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

如果我的日期是= =,一切都可以正常运行,如果我的日期超过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将给我错误,12/12/2014不会

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

我怀疑我的视图模式预期日期格式为mm / dd / yyyy while在我的datepicker我指定dd / mm / yy,有没有办法告诉我的视图模型,我期待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 globalize 或将以下内容添加到脚本中(假设服务器文化日期格式为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 => m.JobStartDate )

这篇关于jquery ui datepicker和mvc视图模型类型datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆