该字段必须是日期 - 在Chrome的DatePicker验证失败 - MVC [英] The field must be a date - DatePicker validation fails in Chrome - mvc

查看:414
本文介绍了该字段必须是日期 - 在Chrome的DatePicker验证失败 - MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有奇怪的问题。我的日期验证不会在Chrome工作。我试过<一个href=\"http://stackoverflow.com/questions/15706455/the-field-date-must-be-a-date-in-mvc-in-chrome\">this回答但它并没有为我工作。

我有这个在我的模型:

  [显示(名称=日期)]
[数据类型(DataType.Date)
公众的DateTime日期{搞定;组; }

我的观点:

 &LT; D​​IV CLASS =主编场&GT;
@ Html.TextBoxFor(型号=&GT; model.Item.Date,新{@class =选择器})
@ Html.ValidationMessageFor(型号=&GT; model.Item.Date)
&LT; / DIV&GT;$(文件)。就绪(函数(){
    $('。选择器')。日期选择器({
        DATEFORMAT:DD.MM.YY,
        changeMonth:真实,
        changeYear:真实,
        selectOtherMonths:真
    });
});

一切工作在Opera和Firefox浏览器,但不喜欢这种类型的日期。我经常收到以下错误字段'日期'必须是日期

任何想法?

更新

这似乎是有问题时,code是一个局部视图中。当我复制code到主网页,确认工作正常。

我插入我的主视图内局部视图仅仅是这样的:

  @ Html.Partial(_ CreateOrEdit模型)

这$ C $上面c是一个局部视图中:

  @model Pro.Web.Models.Model&LT; D​​IV CLASS =主编场&GT;
@ Html.TextBoxFor(型号=&GT; model.Item.Date,新{@class =选择器})
@ Html.ValidationMessageFor(型号=&GT; model.Item.Date)


    

  $(文件)。就绪(函数(){
$('。选择器')。日期选择器({
    DATEFORMAT:DD.MM.YY,
    changeMonth:真实,
    changeYear:真实,
    selectOtherMonths:真
});&LT; / SCRIPT&GT;
});

UPDATE2

这并不毕竟工作。有时工作,有时没有。我点击日期几次,有时验证通过。对于同一日期有可能是好的,错的验证。

这是日期字段输出HTML:

 &LT; D​​IV CLASS =主编场&GT;
&LT;输入类=选择器的数据-VAL =真正的数据-VAL-日期=字段的日期必须是日期。数据-VAL-所需=是必需的日期字段。 ID =日期NAME =Item.Date类型=文本VALUE =1.1.0001 0:00:00/&GT;
&LT;跨度类=现场验证,有效的数据valmsg换=Item.Date数据valmsg替换=真正的&GT;&LT; / SPAN&GT;
&LT; / DIV&GT;


解决方案

在这里精确相同的情况下(部分图)

我解决它通过消除验证属性,并在服务器端执行验证:

  $('#日期)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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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