2009-04-17 32 views
5

我正在使用NerdDinner樣本application,併到達處理虛擬地球地圖的部分。該應用程序存儲經度和緯度的一些值。不幸的是,在我的系統中,浮點數以逗號作爲小數點分隔符存儲,而不是像美國那樣的點。所以如果我的緯度是47.64,它會被檢索並顯示爲47,64。因爲該值在函數調用中傳遞給虛擬地球API,所以它在此時失敗(例如,JavaScript API預計會得到47.64,-122.13,但會獲得47,64,-122,13)。如何全球化ASP.NET MVC視圖(特別是小數分隔符)?

我需要確保應用程序始終使用點。在WebForms應用程序中,我會有一個覆蓋System.Web.UI.Page.InitializeCulture()方法的公共類,並且我將從該類繼承我的頁面。

我不知道如何做到這一點與MVC。我需要一個定製的ViewPage什麼的?有沒有一個容易方式來解決這個問題?例子?

+0

看來,這已經記錄爲錯誤:http ://nerddinner.codeplex.com/WorkItem/View.aspx?WorkItemId = 2616 – 2009-04-17 16:17:32

+0

嗯。也許這是一個錯誤?我在我的web.config文件中添加了,以確保文化被設置爲en-US應用程序範圍,但是根本沒有任何幫助,只有在我發佈後才發佈。 – 2009-04-23 13:12:59

回答

2

因爲設置<globalization/>到en-US根本沒有幫助,我決定創建一個自定義類,它初始化正確的文化設置並確保所有需要此行爲的視圖都從我的自定義類繼承。

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>" %> 
0

當你說

在我的系統浮點數存儲用逗號作爲小數點分隔

我假設你的意思是他們是格式化用逗號,浮點號碼存儲爲float

雖然您可以通過設置文化來解決格式問題,但「真正的」修復方法是更改​​代碼。好吧,這不是你的代碼,所以也許你不想在這個場合做到這一點,但對於一般的參考你需要確保格式化浮動或其他任何你使用適當的文化。在爲一個API使用的數字格式化的情況下,您可以使用InvariantCulture。

I.e.使用foo.ToString(CultureInfo.InvariantCulture)而不是foo.ToString(),同樣使用string.Format(...)。

編輯我只是看看NerdDinner代碼,並已意識到這個錯誤是在Javascript中不在C#中,所以我上面的代碼是不會幫助。我不知道是否可以在Javascript中格式化數字,但我認爲真正的解決方案是修復模型代碼以返回格式正確的字符串。

編輯2我建議你嘗試以下操作: 在SearchController.cs變化JsonDinnerLatitudeLongitudestrings。即

public class JsonDinner { 
    public int  DinnerID { get; set; } 
    public string Title  { get; set; } 
    public string Latitude { get; set; } 
    public string Longitude { get; set; } 
    public string Description { get; set; } 
    public int  RSVPCount { get; set; } 
} 

然後向下滾動到SearchByLocation方法,改變緯度/龍線正確格式化字符串的JavaScript:

Latitude = dinner.Latitude.ToString(CultureInfo.InvariantCulture), 
Longitude = dinner.Longitude.ToString(CultureInfo.InvariantCulture), 

這應該意味着你不需要你把修復,並應該解決你的其他問題......我將在哪裏發表評論。希望這可以幫助,我還沒有完全測試,因爲我不在你的語言環境,但它肯定似乎工作。

2

我是一個丹麥的開發商和麪臨完全一樣的問題。我發現已經由克里斯托夫Neirynck在他的博客開發敬請描述的工作液:

Custom Model Binder

最好的問候,芬蘭人Vilsbaek

0

我使用的TemplateEditor一個簡單的quickfix。我的應用程序只使用瑞典語(逗號作爲小數點分隔符),所以它是一個單一的字符串。更換,但你當然可以讓它知道多種文化。

在我的意見/共享/ EditorTemplates/Decimal.ascx:(。)

0

我解決了這個在JavaScript的一側,而不是,確保什麼是傳遞到地圖 - 庫使用點,並且填充到文本框中的內容使用逗號(,)。顯然,這不是爲了本地化,而是快速修復。

Map.js在callbackForLocation:

//If we've found exactly one place, that's our address. 
if (points.length === 1) { 
    $("#Latitude").val(points[0].Latitude.toString().replace('.', ',')); 
    $("#Longitude").val(points[0].Longitude.toString().replace('.', ',')); 
} 

Map.ascx在 jQuery的準備():

var latitude = <%=Model.Latitude.ToString().Replace(',', '.')%>; 
    var longitude = <%=Model.Longitude.ToString().Replace(',', '.')%>;