问题描述
我有奇怪的问题。我的日期验证不会在Chrome工作。我试过this回答但它并没有为我工作。
我有这个在我的模型:
[显示(名称=日期)]
[数据类型(DataType.Date)
公众的DateTime日期{搞定;组; }
我的观点:
< DIV CLASS =主编场>
@ Html.TextBoxFor(型号=> model.Item.Date,新{@class =选择器})
@ Html.ValidationMessageFor(型号=> model.Item.Date)
< / DIV>$(文件)。就绪(函数(){
$('。选择器')。日期选择器({
DATEFORMAT:DD.MM.YY,
changeMonth:真实,
changeYear:真实,
selectOtherMonths:真
});
});
一切工作在Opera和Firefox浏览器,但不喜欢这种类型的日期。我经常收到以下错误字段'日期'必须是日期
。
任何想法?
更新
这似乎是有问题时,code是一个局部视图中。当我复制code到主网页,确认工作正常。
我插入我的主视图内局部视图仅仅是这样的:
@ Html.Partial(_ CreateOrEdit模型)
这$ C $上面c是一个局部视图中:
@model Pro.Web.Models.Model< DIV CLASS =主编场>
@ Html.TextBoxFor(型号=> model.Item.Date,新{@class =选择器})
@ Html.ValidationMessageFor(型号=> model.Item.Date)
$(文件)。就绪(函数(){
$('。选择器')。日期选择器({
DATEFORMAT:DD.MM.YY,
changeMonth:真实,
changeYear:真实,
selectOtherMonths:真
});< / SCRIPT>
});
UPDATE2
这并不毕竟工作。有时工作,有时没有。我点击日期几次,有时验证通过。对于同一日期有可能是好的,错的验证。
这是日期字段输出HTML:
< DIV CLASS =主编场>
<输入类=选择器的数据-VAL =真正的数据-VAL-日期=字段的日期必须是日期。数据-VAL-所需=是必需的日期字段。 ID =日期NAME =Item.Date类型=文本VALUE =1.1.0001 0:00:00/>
<跨度类=现场验证,有效的数据valmsg换=Item.Date数据valmsg替换=真正的>< / SPAN>
< / DIV>
在这里精确相同的情况下(部分图)
我解决它通过消除验证属性,并在服务器端执行验证:
$('#日期)removeAttr(数据-VAL-DATE)。
I have strange problem. My date validation doesn't work in Chrome. I've tried this answer but it didn't work for me.
I have this in my model:
[Display(Name = "Date")]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
My View:
<div class="editor-field">
@Html.TextBoxFor(model => model.Item.Date, new { @class = "picker" })
@Html.ValidationMessageFor(model => model.Item.Date)
</div>
$(document).ready(function () {
$('.picker').datepicker({
dateFormat: 'dd.mm.yy',
changeMonth: true,
changeYear: true,
selectOtherMonths: true
});
});
Everything works in Opera and Firefox but Chrome doesn't like this type of date. I'm constantly getting the following error The field 'Date' must be a date
.
Any ideas?
UPDATE
It seems there is a problem when the code is inside a partial view. When I copy that code to a main page, validation works fine.
I'm inserting partial view inside my main View simply like this:
@Html.Partial("_CreateOrEdit", Model)
And that code above is inside a partial view:
@model Pro.Web.Models.Model
<div class="editor-field">
@Html.TextBoxFor(model => model.Item.Date, new { @class = "picker" })
@Html.ValidationMessageFor(model => model.Item.Date)
$(document).ready(function () {
$('.picker').datepicker({
dateFormat: 'dd.mm.yy',
changeMonth: true,
changeYear: true,
selectOtherMonths: true
});
</script>
});
UPDATE2
It doesn't work after all. Sometimes it works, sometimes not. I clicked a couple of times on dates and sometimes validation passes. For the same date there could be good and wrong validation.
This is output html for date field:
<div class="editor-field">
<input class="picker" data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="date" name="Item.Date" type="text" value="1.1.0001. 0:00:00" />
<span class="field-validation-valid" data-valmsg-for="Item.Date" data-valmsg-replace="true"></span>
</div>
Exact the same situation here (partial view)
I solved it by removing the validation attribute and perform validation at server side:
$('#date').removeAttr("data-val-date");
这篇关于该字段必须是日期 - 在Chrome的DatePicker验证失败 - MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!