2011-09-16 36 views
1

看起來像我在ASP.NET MVC2項目中使用的JsonValueProviderFactory(來自Microsoft.Web.Mvc)是文化特定的。System.Web.Mvc.JsonValueProviderFactory文化特定?

我從客戶端發送此JSON:

{ 
    "name": "Max", 
    "weight": "60.21" 
} 

JsonValueProviderFactory轉換JSON到這個類:

public class User 
{ 
    public string Name { get; set; } 
    public double Weight { get; set; } 
} 

一切正常,如果我現在的文化是CultureInfo.InvariantCulture。但是,如果我明確地把我的文化「RU-RU」用戶的體重值是0.0

好吧,我可以給格式化基於文化的體重從客戶價值,例如:

{ 
    "name": "Max", 
    "weight": "1,100.21" // it's just example, not my real weight :) 
} 

但在這種情況JsonValueProviderFactory無法解析權重,它將爲零,儘管double.Parse(「1,100.21」,CultureInfo.CurrentCulture)可以正常工作。

我該如何解決這個問題?

回答

1

如果weight屬性應該是雙倍,把它作爲這樣,還不如字符串:

var model = { 
    "name": "Max", 
    "weight": 60.21 
}; 

$.ajax({ 
    url: '@Url.Action("someAction")', 
    type: 'POST', 
    contentType: 'application/json', 
    data: JSON.stringify(model), 
    success: function (result) { 
     alert(result); 
    } 
}); 
+0

它的工作原理,謝謝! – maxvasil