views:

771

answers:

3

Hi people. I am using a ASP.NET MVC framework, jQuery library (1.3.2), linq to sql, sql server 2005.

I have a problem with jQuery MaskMoney plugin, to save data in sql server table.

I created a table, where this table have a field "valueProducts" type Decimal(18,2).

In my page, I have a html field with jQuery maskmoney. Example: 10.000,00 (Ten thousand (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).

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.

A: 

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.

I would do some data parsing/checking on the server side anyway just for security reasons.

Me1000
A: 

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.

See this question too: does SQL Server consider culture/locale when converting values to/from strings?

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

gbn
+1  A: 

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.

The solution that I found was...

I don't save money fields of the View using UpdateModel.

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.

//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.

Renato Bezerra