I have a DateTime
property in my model, but I'm only interested in the time part:
我的模型中有一个DateTime属性,但我只对时间部分感兴趣:
public class MyModel
{
[Required, DataType.Time]
public DateTime Time
}
In my view, I output it like so:
在我看来,我这样输出:
@Html.EditorFor(model => model.Time)
This used to generate the following html in MVC 3:
这用于在MVC 3中生成以下html:
<input class="text-box single-line" data-val="true"
data-val-required="The Time field is required."
id="Time" name="Time" type="time" value="" />
Now I'm trying this with MVC 4, and this is the html generated:
现在我正在尝试使用MVC 4,这是生成的html:
<input class="text-box single-line" data-val="true"
data-val-date="The field Time must be a date."
data-val-required="The Time field is required."
id="Time" name="Time" type="time" value="" />
There is an extra attribute data-val-date
added, which results in a client side validation error, because a time (e.g. "10:30") isn't formated as a date.
添加了额外的属性data-val-date,这导致客户端验证错误,因为时间(例如“10:30”)未形成为日期。
I suppose this is a bug in MVC 4 (I reported it)? But until it's resolved, is there any way to prevent this attribute from being generated? Or even better, is there a way to let it be generated as a data-val-time
in stead, so I can provide my own client side validation method for time fields?
我想这是MVC 4中的一个错误(我报告过)?但是在它解决之前,有没有办法阻止生成这个属性?或者更好的是,有没有办法让它作为数据值时间生成,所以我可以为时间字段提供自己的客户端验证方法?
5 个解决方案
#1
4
Not ideal but it will do the job; use JavaScript in your master template to remove the validation:
不理想,但它会完成这项工作;在主模板中使用JavaScript来删除验证:
$(document).ready(function(){
$("input[data-val-date]").removeAttr("data-val-date");
});
#2
3
Use [DataType("Time")] instead of [DataType(DataType.DateTime)]. This will make MVC not treat your variable as DateTime, and then it will not generate the "data-val-date" attribute.
使用[DataType(“Time”)]代替[DataType(DataType.DateTime)]。这将使MVC不将您的变量视为DateTime,然后它将不会生成“data-val-date”属性。
Essentially [DataType("Time")] makes your variable a custom type. However, you cannot use any other string name for the custom type. MVC tries to detect the underlying type for a custom type. Only when you name your custom type as "Time", it will give up the detection.
基本上[DataType(“Time”)]使您的变量成为自定义类型。但是,您不能使用任何其他字符串名称作为自定义类型。 MVC尝试检测自定义类型的基础类型。只有当您将自定义类型命名为“时间”时,它才会放弃检测。
#3
2
I can say better option will be to write your own client side validation function. Otherwise you can do some spoofing which I wont say it is right way but it can solve your issue. I had similar issue with respect to MVC validation where I wanted to change the behavior of client side validation. Please see the link
我可以说更好的选择是编写自己的客户端验证功能。否则你可以做一些欺骗,我不会说这是正确的方式,但它可以解决你的问题。我有关于MVC验证的类似问题,我想改变客户端验证的行为。请看链接
Asp.net MVC3验证
The idea here is you manually add attributes such as "data-val-required" and "data-val" to your input field and then add validation message for it.
这里的想法是您手动将诸如“data-val-required”和“data-val”之类的属性添加到输入字段,然后为其添加验证消息。
Hope it helps you in some way.
希望它能以某种方式帮助你。
Edit:
编辑:
try this
尝试这个
Remove required field attr from model
从模型中删除必填字段attr
public class MyModel
{
public DateTime Time
}
Then add attributes to your control as shown below
然后向控件添加属性,如下所示
@Html.TextBoxFor(model => model.Time, new Dictionary<string, object> { { "data-val-required", "required" }, { "data-val", "true" }})
@Html.ValidationMessage("Time", "The Time field is required.")
#4
1
I have tried setting the value of the input field as a javascript date object, which in turn passed the validation of the unobtrusive validation.
我已经尝试将输入字段的值设置为javascript日期对象,而后者又通过了不显眼的验证的验证。
$("#date-input-field").val(new Date(1,0,1970));
$.validator.unobtrusive.parse('#form');
var form = $('#form');
form.validate();
This is done with jQuery 1.7.2, validation.unobtrusive and mvc4.
这是通过jQuery 1.7.2,validation.unobtrusive和mvc4完成的。
Hope this helps.
希望这可以帮助。
#5
1
The best way is to set ClientValidationEnabled
and UnobtrusiveJavaScriptEnabled
to false in the HtmlHelper class.
最好的方法是在HtmlHelper类中将ClientValidationEnabled和UnobtrusiveJavaScriptEnabled设置为false。
HtmlHelper.ClientValidationEnabled = false;
HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
#1
4
Not ideal but it will do the job; use JavaScript in your master template to remove the validation:
不理想,但它会完成这项工作;在主模板中使用JavaScript来删除验证:
$(document).ready(function(){
$("input[data-val-date]").removeAttr("data-val-date");
});
#2
3
Use [DataType("Time")] instead of [DataType(DataType.DateTime)]. This will make MVC not treat your variable as DateTime, and then it will not generate the "data-val-date" attribute.
使用[DataType(“Time”)]代替[DataType(DataType.DateTime)]。这将使MVC不将您的变量视为DateTime,然后它将不会生成“data-val-date”属性。
Essentially [DataType("Time")] makes your variable a custom type. However, you cannot use any other string name for the custom type. MVC tries to detect the underlying type for a custom type. Only when you name your custom type as "Time", it will give up the detection.
基本上[DataType(“Time”)]使您的变量成为自定义类型。但是,您不能使用任何其他字符串名称作为自定义类型。 MVC尝试检测自定义类型的基础类型。只有当您将自定义类型命名为“时间”时,它才会放弃检测。
#3
2
I can say better option will be to write your own client side validation function. Otherwise you can do some spoofing which I wont say it is right way but it can solve your issue. I had similar issue with respect to MVC validation where I wanted to change the behavior of client side validation. Please see the link
我可以说更好的选择是编写自己的客户端验证功能。否则你可以做一些欺骗,我不会说这是正确的方式,但它可以解决你的问题。我有关于MVC验证的类似问题,我想改变客户端验证的行为。请看链接
Asp.net MVC3验证
The idea here is you manually add attributes such as "data-val-required" and "data-val" to your input field and then add validation message for it.
这里的想法是您手动将诸如“data-val-required”和“data-val”之类的属性添加到输入字段,然后为其添加验证消息。
Hope it helps you in some way.
希望它能以某种方式帮助你。
Edit:
编辑:
try this
尝试这个
Remove required field attr from model
从模型中删除必填字段attr
public class MyModel
{
public DateTime Time
}
Then add attributes to your control as shown below
然后向控件添加属性,如下所示
@Html.TextBoxFor(model => model.Time, new Dictionary<string, object> { { "data-val-required", "required" }, { "data-val", "true" }})
@Html.ValidationMessage("Time", "The Time field is required.")
#4
1
I have tried setting the value of the input field as a javascript date object, which in turn passed the validation of the unobtrusive validation.
我已经尝试将输入字段的值设置为javascript日期对象,而后者又通过了不显眼的验证的验证。
$("#date-input-field").val(new Date(1,0,1970));
$.validator.unobtrusive.parse('#form');
var form = $('#form');
form.validate();
This is done with jQuery 1.7.2, validation.unobtrusive and mvc4.
这是通过jQuery 1.7.2,validation.unobtrusive和mvc4完成的。
Hope this helps.
希望这可以帮助。
#5
1
The best way is to set ClientValidationEnabled
and UnobtrusiveJavaScriptEnabled
to false in the HtmlHelper class.
最好的方法是在HtmlHelper类中将ClientValidationEnabled和UnobtrusiveJavaScriptEnabled设置为false。
HtmlHelper.ClientValidationEnabled = false;
HtmlHelper.UnobtrusiveJavaScriptEnabled = false;