C# MVC3:提交字符串-在IE和Chrome上解析失败
我对提交的表格信息有疑问。当我试图解析通过Interner Explorer或Chrome返回的字符串,但在Firefox或Safari上不解析时,十进制解析失败。字符串在VisualStudio中看起来完全相同。我做了如下调试:C# MVC3:提交字符串-在IE和Chrome上解析失败,c#,string,asp.net-mvc-3,parsing,C#,String,Asp.net Mvc 3,Parsing,我对提交的表格信息有疑问。当我试图解析通过Interner Explorer或Chrome返回的字符串,但在Firefox或Safari上不解析时,十进制解析失败。字符串在VisualStudio中看起来完全相同。我做了如下调试: var asd3 = collection["formValue"]; // Get it from the FormCollection var asd4 = asd3.Replace(",", ".");
var asd3 = collection["formValue"]; // Get it from the FormCollection
var asd4 = asd3.Replace(",", "."); // Change the punctuation
var asd5 = Decimal.Parse(asd4); // Make the string into a decimal
var asd6 = Math.Round(asd5, 1); // Round it
当尝试解析asd4
中的十进制时,asd5
失败,错误为:输入字符串格式不正确。
这是字符串的图像。最上面是Firefox,下面是InternetExplorer
这里到底有什么问题
这里到底有什么问题
文化
在调试器中,检查Thread.CurrentThread.CurrentCulture的值,您将看到浏览器之间的差异
如果您的浏览器中设置了不同的区域性,ASP.NET在解析值时将使用此区域性,特别是如果您没有在web.config中明确指定区域性:
<globalization culture="en-US" uiCulture="en-US" />
始终使用特定区域性信息分析数字。示例:“Decimal.Parse(number,new CultureInfo(“en-US”))”。如果两个示例中的输入都是“5.5”,那么当您指定区域性信息时,它将永远不会失败。是!这就解决了这个问题,将web.config的区域性从自动更改为定义的区域性。非常感谢。
var asd5 = Decimal.Parse(asd4, CultureInfo.InvariantCulture);