我对提交的表单信息有疑问。当我尝试解析通过 Interner Explorer 或 Chrome 而不是 Firefox 或 Safari 返回的字符串时,十进制解析失败。这些字符串在 Visual Studio 中看起来完全一样。我做了这个调试位:
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
上失败,并出现错误:Input string was not in a correct format.
这是字符串的图像。顶部是 Firefox,下面是 Internet Explorer。
这到底是什么问题?
最佳答案
What on earth could be the problem here?
文化。
在调试器中检查 Thread.CurrentThread.CurrentCulture
的值,您将看到浏览器之间的差异。
如果您在浏览器中设置了不同的文化,ASP.NET 将在解析值时使用此文化,尤其是在您未在 web.config 中明确指定文化的情况下:
<globalization culture="en-US" uiCulture="en-US" />
如果设置为 auto
则将使用浏览器文化。
另一种可能性是在解析时强制文化不变,以确保 .
(点)将成为小数点分隔符。
var asd5 = Decimal.Parse(asd4, CultureInfo.InvariantCulture);
关于c# - MVC3 : Submit string - parse fails on IE and Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9889644/