c# - MVC3 : Submit string - parse fails on IE and Chrome

标签 c# string asp.net-mvc-3 parsing

我对提交的表单信息有疑问。当我尝试解析通过 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。

enter image description here

这到底是什么问题?

最佳答案

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/

相关文章:

c# - 添加 boolean 值(作为整数)

c# - 带有方括号的模型绑定(bind) POST 值

python - 在 Python 中看到字符之前,如何删除字符串中的所有内容

asp.net - MVC3 和 Entity Framework

c# - 不支持 NTLM 身份验证 token

c# - 在 C# 中获取两次之间的时间跨度?

php - 替换字符串或 explode() 函数

c - (int, const char *) 作为 uthash 库的复合键

c# - 在 ASP.NET MVC3 中使用 C# 填充 Word 模板

c# - 如何跳过 sqldatareader 中的第一行