带有jquery验证和Html.EditorFor和不同的小数分隔符的asp.net mvc [英] asp.net mvc with jquery validation and Html.EditorFor and different decimalseparators
问题描述
我已经阅读了很多有关此主题的博客文章,但是我从未看到完整的解决方案.
I've read a lot of blogposts about this subject, but I never saw the complete solution.
我已经使用了默认的html(其中bedrag是一个十进制):
I've scaffolfed this default html (where bedrag is a Decimal):
<div class="editor-field">
@Html.EditorFor(model => model.Bedrag)
@Html.ValidationMessageFor(model => model.Bedrag)
</div>
呈现页面时,即使在我将其放在web.config中时,这也会在输入控件中为小数点生成一个逗号:
<globalization culture="en-US" uiCulture="en-US" />
或这个
<globalization culture="auto" uiCulture="auto" />
This generates a comma for the decimal in the input control when the page is rendered, even when I put this in my web.config:
<globalization culture="en-US" uiCulture="en-US" />
or this one
<globalization culture="auto" uiCulture="auto" />
然后,Jquery Validation插件抱怨我在'bedrag'输入字段(已由@Html.EditorFor(model => model.Bedrag)
放在该字段中)中用逗号作为小数点分隔符,这给我输入的内容不是数字提供了错误;它希望以点作为小数点分隔符.
Then, the Jquery Validation plugin complains that I have a comma as decimal separator in my 'bedrag' input field (which has been put there by the @Html.EditorFor(model => model.Bedrag)
), which gives the error my input is not a number; it expects a dot as a decimal separator.
但是,当我输入此数字12.90时,默认的modelbinder会将我的输入转换为1290(输入乘以100).
But, when I enter the number as this 12.90, the defaults modelbinder converts my input to 1290 (input times 100).
然后我创建了一个自定义modelbinder,在该代码中,当前区域性是'nl-NL',而不是我的web.config中的'en-US'.
Then I created a custom modelbinder, and in that code, the currentculture is 'nl-NL', so not the 'en-US' from my web.config.
所以现在我想知道:
1 2014年我仍然需要自定义模型活页夹吗?
1 Do I still need a custom model binder in 2014?
2 ASP.Net为@Html.ValidationMessageFor(model => model.Bedrag)
创建值时使用哪种文化? (为什么不从我的web.config中选择一个?)
2 Which culture is used when ASP.Net creates the value for @Html.ValidationMessageFor(model => model.Bedrag)
? (and why not the one from my web.config?)
3如何动态设置要用于@Html.ValidationMessageFor(model => model.Bedrag)
3 How can I dynamicly set the culture to use for the @Html.ValidationMessageFor(model => model.Bedrag)
4如何动态设置用于Jquery验证的区域性?
4 How can I dynamicly set the culture to use for the Jquery validation?
我已经退出ASP.Net MVC一年了,但是在2014年,我仍然会遇到这些带有小数点的问题吗?
I've been out of ASP.Net MVC for a year, but can it be that in 2014 I still have these issues with decimal signs?
推荐答案
我使用的方法是扩展jQuery验证程序.它基于此博客文章.我将元数据中的区域性设置为BE-nl.由于这是一个纯荷兰语的网站,因此我不再做任何检查.
The approach I used is to extend the jQuery validator. It is based on this blogpost. I setted the culture in the metadata to BE-nl. Since this is a pure Dutch website, I don't do any checks further on.
$(function () {
// Look in metatag what culture we want
// and set this as culture for the client side.
var data = $("meta[name='accept-language']").attr("content");
Globalize.culture(data.toString());
// Don't validate on keyup event because it will mess up
// the cursor when replacing values in a textbox.
$('form').each(function () {
var validator = $(this).data('validator');
if (validator) {
validator.settings.onkeyup = false;
}
});
// Belgianize/sanitize the numbers inserted
// 1 000 000 => 1000000
// 1.00 => 1,00
$.validator.methods.number = function (value, element) {
var s = value.replace(/\ /g, '').split('.').join(',');
if (s.split(',').length < 3) {
var number = Globalize.parseFloat(s);
if (!isNaN(number)) {
$(element).val(s);
return this.optional(element) || true;
}
}
return this.optional(element) || false;
};
});
我认为我使用此jQuery库进行了全球化
I think I used this jQuery library for the globalization
这篇关于带有jquery验证和Html.EditorFor和不同的小数分隔符的asp.net mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!