如何全球化ASP.NET MVC的意见(特别是小数点分隔符)? [英] How to globalize ASP.NET MVC views (decimal separators in particular)?

查看:188
本文介绍了如何全球化ASP.NET MVC的意见(特别是小数点分隔符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的的NerdDinner 样本应用程序的工作,抵达它与虚拟地球地图涉及的部分。应用程序存储用于经度和纬度中的值。不幸的是在我的系统浮点数存储用逗号作为小数点分隔符,而不是像在美国的一个点。所以,如果我有一个47.64纬度它的检索和显示47,64。因为这个值在函数调用传递到虚拟地球API失败在这一点上(如JavaScript的API预计的 47.64,-122.13 的,但得到的 47,64,-122,13 )。

I'm working with the NerdDinner sample application and arrived at the section which deals with the Virtual Earth map. The application stores some values for the longitude and latitude. Unfortunately on my system floating point numbers are stored with a comma as the decimal separator, not a dot like in the US. So if I have a latitude of 47.64 it's retrieved and displayed as 47,64. Because that value is passed in a function call to the Virtual Earth API it fails at that point (e.g. JavaScript API expects 47.64, -122.13, but gets 47,64, -122,13).

我需要确保应用程序始终使用圆点。在一个应用程序的WebForms我不得不将覆盖 System.Web.UI.Page.InitializeCulture()方法的通用类,我会从该类继承被我的网页。

I need to make sure that the application always uses dots. In a WebForms app I would have a common class which overrides the System.Web.UI.Page.InitializeCulture() method and I would be inheriting my pages from that class.

我不知道如何做同样的MVC。我需要一个定制的的ViewPage 还是什么?是否有一个轻松的方式来解决这个问题?例子?

I am not sure about how to do the same with MVC. Do I need a customized ViewPage or something? Is there an easy way to solve this? Examples?

推荐答案

由于设置<全球化/> 为en-US没有在所有帮助我决定创建一个初始化正确的文化设置的自定义类,并确保需要这种行为的所有观点都是从我的自定义类继承而来的。

Because setting <globalization/> to en-US did not help at all I decided to create a custom class which initializes the proper culture settings and make sure that all views which require this behavior are inherited from my custom class.

NerdDinnerViewPage.cs:

using System.Globalization;
using System.Threading;
using System.Web.Mvc;

namespace NerdDinner.Views
{
    public class NerdDinnerViewPage<T> : ViewPage<T> where T : class
    {
        protected override void InitializeCulture()
        {
            base.InitializeCulture();

            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;

            if (Thread.CurrentThread.CurrentCulture != null)
            {
                Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator = ".";
                Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
            }
        }
    }
}

Edit.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="NerdDinner.Views.NerdDinnerViewPage<NerdDinner.Models.DinnerFormViewModel>" %>

这篇关于如何全球化ASP.NET MVC的意见(特别是小数点分隔符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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