I am using a ASP.NET MVC framework, jQuery library (1.3.2), linq to sql, sql server 2005.
我使用的是ASP.NET MVC框架,jQuery库(1.3.2),linq to sql,sql server 2005。
I have a problem with jQuery MaskMoney plugin, to save data in sql server table.
我有一个jQuery MaskMoney插件的问题,在sql server表中保存数据。
I created a table, where this table have a field "valueProducts" type Decimal(18,2).
我创建了一个表,这个表有一个字段“valueProducts”类型Decimal(18,2)。
In my page, I have a html field with jQuery maskmoney. Example: 10.000,00 (Ten thousand (R$)).
在我的页面中,我有一个带有jQuery maskmoney的html字段。示例:10.000,00(一万(R $))。
When I save this data, occurs this error: "The model of type 'MercanteWeb.Dados.MateriasPrimasEntradas' was not successfully updated." (I use UpdateModel to save data).
当我保存这些数据时,会出现此错误:“类型'MercanteWeb.Dados.MateriasPrimasEntradas'的模型未成功更新。” (我使用UpdateModel来保存数据)。
I found out that this problem occurs because of thousand separator (.) and if I remove this mask plugin, it is work.
我发现这个问题是因为千位分隔符(。)而发生的,如果我删除这个掩码插件,那就行了。
Someone can help me?
有人可以帮帮我吗?
Thanks.
4 个解决方案
#1
I did it.
我做的。
The problem of answer above is in the Linq to SQL data mapping. Fields type money or smallmoney are mapping using decimal type.
上面的答案问题是在Linq to SQL数据映射中。字段类型money或smallmoney使用十进制类型进行映射。
The solution that I found was...
我发现的解决方案是......
I don't save money fields of the View using UpdateModel.
我不使用UpdateModel保存View的money字段。
before:
Check c = new Check();
this.UpdateModel(c, new[] { "number", "name", "value1", "value2" });
after:
Check c = new Check();
this.UpdateModel(c, new[] { "number", "name" });
c.value1 = Convert.ToDecimal(f["value1"]);
c.value2 = Convert.ToDecimal(f["value2"]);
With this, the value is successfully convert for decimal data type.
这样,该值成功转换为十进制数据类型。
In the view, I used the objects of Globalization namespace.
在视图中,我使用了Globalization命名空间的对象。
//ViewPage
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.Globalization" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<%
NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
LocalFormat.CurrencySymbol = "";
Check c = (Check)ViewData.Model;
%>
//In the field, I use de string format type currency and I passed the object of //CurrentSymbol
...
Value1:<br>
<%= Html.TextBox("value1", Convert.ToDouble(c.value1).ToString("C", LocalFormat))%>
....
</asp:Content>
Thank you for helping me.
感谢你们对我的帮助。
#2
You can either remove the separators:
您可以删除分隔符:
$("#real").maskMoney({symbol:"R$",decimal:"",thousands:""});
Or parse through the number in asp.net removing them and it will work. I'm not familiar with asp.net so I couldn't give you the syntax.
或者解析通过asp.net中的数字删除它们,它会起作用。我不熟悉asp.net所以我无法给你语法。
I would do some data parsing/checking on the server side anyway just for security reasons.
出于安全原因,我还是会在服务器端进行一些数据解析/检查。
#3
If the data is being written directly to a database as a string, SQL Server supports the basic UK/US number formats only eg 10000.00. You'll have to clean the data into either a basic format or into a numeric format before going to SQL Server.
如果数据作为字符串直接写入数据库,则SQL Server仅支持基本的英国/美国数字格式,例如10000.00。在转到SQL Server之前,您必须将数据清理为基本格式或数字格式。
See this question too: does SQL Server consider culture/locale when converting values to/from strings?
也看到这个问题:SQL Server在将值转换为字符串时是否考虑了culture / locale?
Dates and text sorting can adjusted to locales etc, but not number formats.
日期和文本排序可以调整为区域设置等,但不是数字格式。
Also, I'd suggest that money or smallmoney would be a better datatype from both efficiency and performance: float vs decimal vs money datatype article and flowchart
此外,我建议从效率和性能来看,money或smallmoney将是更好的数据类型:float vs decimal vs money数据类型文章和流程图
#4
I'm the maintainer of jquery-maskmoney plugin. As you can see in the plugin homepage you can disable the thousands separator. Something like:
我是jquery-maskmoney插件的维护者。正如您在插件主页中看到的,您可以禁用千位分隔符。就像是:
$("#currency").maskMoney({thousands:""});
#1
I did it.
我做的。
The problem of answer above is in the Linq to SQL data mapping. Fields type money or smallmoney are mapping using decimal type.
上面的答案问题是在Linq to SQL数据映射中。字段类型money或smallmoney使用十进制类型进行映射。
The solution that I found was...
我发现的解决方案是......
I don't save money fields of the View using UpdateModel.
我不使用UpdateModel保存View的money字段。
before:
Check c = new Check();
this.UpdateModel(c, new[] { "number", "name", "value1", "value2" });
after:
Check c = new Check();
this.UpdateModel(c, new[] { "number", "name" });
c.value1 = Convert.ToDecimal(f["value1"]);
c.value2 = Convert.ToDecimal(f["value2"]);
With this, the value is successfully convert for decimal data type.
这样,该值成功转换为十进制数据类型。
In the view, I used the objects of Globalization namespace.
在视图中,我使用了Globalization命名空间的对象。
//ViewPage
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.Globalization" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<%
NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
LocalFormat.CurrencySymbol = "";
Check c = (Check)ViewData.Model;
%>
//In the field, I use de string format type currency and I passed the object of //CurrentSymbol
...
Value1:<br>
<%= Html.TextBox("value1", Convert.ToDouble(c.value1).ToString("C", LocalFormat))%>
....
</asp:Content>
Thank you for helping me.
感谢你们对我的帮助。
#2
You can either remove the separators:
您可以删除分隔符:
$("#real").maskMoney({symbol:"R$",decimal:"",thousands:""});
Or parse through the number in asp.net removing them and it will work. I'm not familiar with asp.net so I couldn't give you the syntax.
或者解析通过asp.net中的数字删除它们,它会起作用。我不熟悉asp.net所以我无法给你语法。
I would do some data parsing/checking on the server side anyway just for security reasons.
出于安全原因,我还是会在服务器端进行一些数据解析/检查。
#3
If the data is being written directly to a database as a string, SQL Server supports the basic UK/US number formats only eg 10000.00. You'll have to clean the data into either a basic format or into a numeric format before going to SQL Server.
如果数据作为字符串直接写入数据库,则SQL Server仅支持基本的英国/美国数字格式,例如10000.00。在转到SQL Server之前,您必须将数据清理为基本格式或数字格式。
See this question too: does SQL Server consider culture/locale when converting values to/from strings?
也看到这个问题:SQL Server在将值转换为字符串时是否考虑了culture / locale?
Dates and text sorting can adjusted to locales etc, but not number formats.
日期和文本排序可以调整为区域设置等,但不是数字格式。
Also, I'd suggest that money or smallmoney would be a better datatype from both efficiency and performance: float vs decimal vs money datatype article and flowchart
此外,我建议从效率和性能来看,money或smallmoney将是更好的数据类型:float vs decimal vs money数据类型文章和流程图
#4
I'm the maintainer of jquery-maskmoney plugin. As you can see in the plugin homepage you can disable the thousands separator. Something like:
我是jquery-maskmoney插件的维护者。正如您在插件主页中看到的,您可以禁用千位分隔符。就像是:
$("#currency").maskMoney({thousands:""});