问题描述
我需要捕捉的日期和时间都为我的模型属性。在我的模型类我有以下
I need to capture date and time both for my model property. In my model class I have the following
[Required]
[DataType(DataType.DateTime)]
public DateTime? CallBackDate { get; set; }
当我输入一个有效的日期和时间(如 28/05/2015 15时55
)我不断收到此错误字段CallBackDate必须一个日期。
When I enter a valid date time (e.g. 28/05/2015 15:55
) I keep getting this error The field CallBackDate must be a date.
我见过类似的问题,并尝试了各种答案,但似乎没有摆脱这一点。我米用不显眼的客户端验证,我不能禁用它。
I have seen similar question and tried various answers but nothing seems to get rid of this. I m using unobtrusive client side validation and I can't disable it.
输入字段的来源有以下标记
The source of the input field has the following markup
<input autocomplete="off" class="jquery_datetimepicker form-control hasDatepicker" data-val="true" data-val-date="The field CallBackDate must be a date." data-val-required="The CallBackDate field is required." id="CallBackDate" name="CallBackDate" placeholder="Enter your CallBackDate" type="text" value="">
和jQuery日期时间选择器具有以下标记
And jquery datetime picker has the following markup
$('.jquery_datetimepicker').datetimepicker({
dateFormat: 'dd/mm/yy',
minDate: 0,
showWeeks: true,
showStatus: true,
highlightWeek: true,
numberOfMonths: 1,
showAnim: "scale",
showOptions: {
origin: ["top", "left"]
},
timeFormat: 'hh:mm tt'
});
任何想法?谢谢
推荐答案
您需要确保您的应用程序的设置正确。
You need to make sure your application's Culture is properly set.
以下示例说明了文化如何影响日期解析:
The following example shows how cultures affect date parsing: https://dotnetfiddle.net/vXQTAZ
DateTime dateValue;
string dateString = "28/05/2015 15:55";
if (DateTime.TryParse(dateString, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, out dateValue))
{
Console.WriteLine("Valid en-US date.");
}
else
{
Console.WriteLine("Not a valid en-US date.");
}
if (DateTime.TryParse(dateString, CultureInfo.CreateSpecificCulture("fr-FR"), DateTimeStyles.None, out dateValue))
{
Console.WriteLine("Valid fr-FR date.");
}
else
{
Console.WriteLine("Not a valid fr-FR date.");
}
输出
不是有效的EN-美国日期。
有效FR-FR日期。
您可能还需要确保您的客户端验证器使用的是正确的文化/全球化。如果您在使用jQuery插件验证与MVC,看到这个期限,以便修改插件来满足您的需求:的
You may also need to make sure that your client side validators are using properly cultures/globalization. If you are using jQuery validate plugin with MVC, see this extension to help modify that plugin to meet your needs: http://blog.icanmakethiswork.io/2012/09/globalize-and-jquery-validate.html
这篇关于如何解决“的字段必须是日期'在MVC中的日期时间属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!