简体   繁体   English

MVC FormCollection 值解析为十进制,使用小数点分隔符

[英]MVC FormCollection value parse to decimal with period seperator for decimals

This time I'm working on a small MVC application.这次我正在开发一个小型 MVC 应用程序。 In the update part of my model in the controller, all the field values are passed through in the controller.在控制器中我的模型的更新部分中,所有字段值都在控制器中传递。 One of the fields is a decimal value.其中一个字段是十进制值。 This value is stored in de the DB using the period as a separator.该值使用句点作为分隔符存储在数据库中。 The field is named ContractHours.该字段名为 ContractHours。 40 is stored as 40.00. 40 存储为 40.00。 Now when retrieving the data the value in the form is showed as 40,00 due to the language setting of my browser (which is fine btw).现在,在检索数据时,由于浏览器的语言设置(顺便说一句,这很好),表单中的值显示为 40,00。 But when I try to update, this value is passed to the formcollection is also 40,00 of course.但是当我尝试更新时,这个值传递给 formcollection 当然也是 40,00。 But when I try to execute the update statement但是当我尝试执行更新语句时

update Employee set ContractHours = collection["ContractHours"] WHERE ID = 1

I get an exception because the DB doesn't accept 40,00 to pass as a decimal value.我得到一个例外,因为数据库不接受 40,00 作为十进制值传递。 So I tried to parse the value as a decimal using:所以我尝试使用以下方法将值解析为小数:

var ch = Decimal.Parse(collection["ContractHours"],CultureInfo.InvariantCulture)

and even乃至

var ch = Decimal.Parse(collection["ContractHours"], NumberStyles.Any, new CultureInfo("en-US"))

But both these statements return the value 40,00 as 4000.但是这两个语句都将值 40,00 返回为 4000。

using a simple replace does the trick, but it doesn't feel right使用简单的替换可以解决问题,但感觉不对

var ch = collection["ContractHours"].Replace(",",".");

I have searched this forum en googled for a solution, but I cannot find any.我已经在这个论坛上搜索了一个解决方案,但我找不到任何解决方案。 So what would be the right way to solve the problem without changing my browser's language settings?那么在不更改浏览器语言设置的情况下解决问题的正确方法是什么?

Today I got in contact with someone who assisted me with this issue.今天我与帮助我解决这个问题的人取得了联系。

It turned out when I used:结果我用的时候:

var ch = Decimal.Parse(collection["ContractHours"], NumberStyles.Any,new CultureInfo("nl-NL"));

Gave me the correct value for the ch var, in this case 40.00.给我正确的 ch var 值,在本例中为 40.00。

However, another problem arose, and this is not part of the original question, but I thought I would share it anyway.然而,出现了另一个问题,这不是原始问题的一部分,但我想无论如何我都会分享它。

I used the ch var in the query-string as shown below:我在查询字符串中使用了 ch var,如下所示:

string sqlQuery = $"update Employee set Name ='{collection["name"]}' , Email = ' {collection["email"]}'," +
                    $"ContractHours={ch},IsActive={(collection["IsActive"].ToString().Contains("true") ? 1 : 0)} " +
                    $"WHERE ID={id}";

When I tried to execute this string, an exception was thrown.当我试图执行这个字符串时,抛出了一个异常。 It turned out that the value of ch was again 40,00 when used in the query-string.事实证明,在查询字符串中使用时,ch 的值再次为 40,00。 I was told that this was caused by the conversion of the var ch to a string and when this happened the value used as the set culture is used.有人告诉我,这是由 var ch 转换为字符串引起的,当发生这种情况时,将使用用作设置区域性的值。 In this case, nl-NL causing the value of ch to change back to 40,00 when used in a string as in the SQL-query.在这种情况下,nl-NL 在 SQL 查询中的字符串中使用时,会导致 ch 的值变回 40,00。

So what we did is we added the next lines in the global.asx.cs of the MVC .NET Framework application in the Applicaiton_OnStart()所以我们所做的是在 Applicaiton_OnStart() 的 MVC .NET Framework 应用程序的 global.asx.cs 中添加下一行

 CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
 CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
 CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
 CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

Then changed the sql-query to:然后将 sql-query 更改为:

string sqlQuery = $"update Employee set Name ='{collection["name"]}' , Email = ' {collection["email"]}'," +
                    $"ContractHours={collection["ContractHours"]},IsActive={(collection["IsActive"].ToString().Contains("true") ? 1 : 0)} " +
                    $"WHERE ID={id}";

There was no need for the var ch anymore.不再需要 var ch 了。 I know the answer answers more than I asked for, but I just added this information, to clarify and I hope someone else can benefit from this.我知道答案的答案比我要求的要多,但我只是添加了这些信息,以进行澄清,我希望其他人可以从中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM