如何使用 MVC2 和 jquery 实现全球化?
我正在 asp.net mvc2 应用程序中尝试一些全球化,但无法让它完全工作。我正在使用库 http://github.com/nje/jquery-glob 作为根据用户意愿显示货币和日期等的工具。然而有些事情让我有点困扰。 IU 无法获得客户端/服务器端验证来实际接受使用 jQuery 的全球化。无论我尝试什么,如果我发回 40.00,一切都会正常,但如果我发回 40,00,它会被接受为 0(不是有效的 .NET 不变十进制值)。我尝试关注 @haacked 的 guide
这是在 global.asax 中用于服务器端处理:
private void SetCulture(string currencySymbol)
{
AjaxHelper.GlobalizationScriptPath =
http://ajax.microsoft.com/ajax/4.0/1/globalization/";
var culturePref = "sv-SE";
var request = HttpContext.Current.Request;
if (request.UserLanguages == null)
return;
var lang = request.UserLanguages[0];
if (lang != null) {
try {
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(lang);
}
catch {
Thread.CurrentThread.CurrentCulture =
new CultureInfo(culturePref);
}
}
Thread.CurrentThread.CurrentUICulture =
Thread.CurrentThread.CurrentCulture;
}
然后在客户端我包含了以下脚本:
<script src="/Scripts/jquery-1.4.2.js" type="text/javascript"></script>
<script src="/Scripts/jquery.glob.js" type="text/javascript"></script>
<script src="/Scripts/globinfo/jquery.glob.sv-SE.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$.culture = jQuery.cultures['sv-SE'];
$.preferCulture('sv-SE');
});
</script>
<script src="/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.pack.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
<script src="/Scripts/jquery.metadata.js" type="text/javascript"></script>
到目前为止很好,但作用不大。不要误会我的意思,我仍然可以在客户端上使用它,但在服务器端什么也没有发生。下面的 jQuery 会相应地更改格式,但是当我发回 40,00 时,它会变成 0.00。
function globalizePage(culture) {
// Set culture from select list
$.preferCulture(culture);
$("input[id$='Date']").val(function () {
var dateString = $(this).val();
var date = Date.parse(dateString);
var dt = $.format(date, 'd', culture.name);
return dt;
});
$("input[id$='Price']").val(function () {
var price = $.parseInt($(this).val());
var retVal = $.format(price, 'c', culture.name);
return retVal;
});
}
我需要做什么才能使上面的代码工作?
I am trying out some globalization in an asp.net mvc2 application but can't get it to work fully. I am using the library http://github.com/nje/jquery-glob as a tool to display currency and dates etc according to user wishes. However something is bothering me a little. IU can't get the client / server side validation to actually accept the globalization using jQuery. No matter what I try if I post back 40.00 everything works ok but if I post 40,00 it is accepted as 0 (not a valid .NET invariant decimal value). I tried to follow @haacked's guide
This is in global.asax for server side handling:
private void SetCulture(string currencySymbol)
{
AjaxHelper.GlobalizationScriptPath =
http://ajax.microsoft.com/ajax/4.0/1/globalization/";
var culturePref = "sv-SE";
var request = HttpContext.Current.Request;
if (request.UserLanguages == null)
return;
var lang = request.UserLanguages[0];
if (lang != null) {
try {
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(lang);
}
catch {
Thread.CurrentThread.CurrentCulture =
new CultureInfo(culturePref);
}
}
Thread.CurrentThread.CurrentUICulture =
Thread.CurrentThread.CurrentCulture;
}
Then on the client side I have included the following scripts:
<script src="/Scripts/jquery-1.4.2.js" type="text/javascript"></script>
<script src="/Scripts/jquery.glob.js" type="text/javascript"></script>
<script src="/Scripts/globinfo/jquery.glob.sv-SE.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$.culture = jQuery.cultures['sv-SE'];
$.preferCulture('sv-SE');
});
</script>
<script src="/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.pack.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
<script src="/Scripts/jquery.metadata.js" type="text/javascript"></script>
So far so good but it doesn't do much. Don't get me wrong I can still get away with using it on the client but nothing really happens on the server side. The below jQuery changes the format's accordingly but when I post back 40,00 it becomes 0.00.
function globalizePage(culture) {
// Set culture from select list
$.preferCulture(culture);
$("input[id$='Date']").val(function () {
var dateString = $(this).val();
var date = Date.parse(dateString);
var dt = $.format(date, 'd', culture.name);
return dt;
});
$("input[id$='Price']").val(function () {
var price = $.parseInt($(this).val());
var retVal = $.format(price, 'c', culture.name);
return retVal;
});
}
What do I need to do to make the above code work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了,培根保存或炸鸡肉。发生这种情况的原因是 jquery.validate 和 jquery.glob 使用名为“format”的函数,如果在 glob 之后添加 validate,则调用 validate.format 函数,这会破坏全球化。另一方面验证功能。我对 javascript 或 jquery 的了解不够好,无法告诉您任何解决方法。我将手动编写表单验证代码,而忽略 MicrosoftMVCjQueryValidation,因为这不能正确执行验证插件。
Problem solved, bacon saved or chicken fried. The reason this is happening is that jquery.validate and jquery.glob uses a function called "format" if validate is added after glob then the validate.format function is called which breaks globalization. On the other hand validate function. I don't know javascript or jquery good enough to tell you any workarounds. I'll just code the validation of the form manually and forget about the MicrosoftMVCjQueryValidation since that doesn't do the validate plugin justice.