How to treat a decimal field with no homes after comma in a jquery mvc c#mask

Asked

Viewed 928 times

1

I have a model with the following definition:

[Display(Name = "Quantidade / Volume:")]
public Int32 RoQuantidade { set; get; }

In my View to fill the data when empty, that is, that have not yet been written, using the maskMoney works smoothly. I use it as follows:

$('#RoQuantidade').maskMoney({ 
        showSymbol: false, 
        symbol: "R$", 
        decimal: ",", 
        thousands: ".", 
        allowZero: true, 
        allowNegative: false, 
        precision: 0 
});

Note that I inform you that the accuracy is zero, as I do not need decimal places in this case.

The problem begins when I will retrieve the information from the data source. In my controller I retrieve the information like this:

model.RoQuantidade = Convert.ToInt32(_sheet.Cells[lin, 3].value2);

I’m picking up the information from an Excel spreadsheet, but that’s not the problem. It turns out that the return to the view even if I remove the mask or try to convert, always return in the following way:

  • original value: 30000
  • Value returned to the view: 30000.00

If I use the maskMoney the way I put it above with an extra parameter that is

$('#Disponivel').maskMoney({ 
        showSymbol: false, 
        symbol: "R$", 
        decimal: ",", 
        thousands: ".",
        allowZero: true, 
        allowNegative: true 
}).maskMoney('mask', $('#Disponivel').val());

to format the returned value it presents the value: 3.000.000, because the field to be decimal added the two zeroes transforming from 30 thousand to 3 million

I’ve tried changing the field setting to int32, however the formatting gets in trouble and at the time of recording informs that there is error as it cannot record 30,000 (note that it changed even with the correct definition the point by the comma).

I tried to use other jquery plugins like maskedit, but I could not, especially when returning the information.

If you have any idea how I can do to fill in the formatted value, record the information without formatting, retrieve the information without formatting and present it in formatted form, it is no problem to change it decimal for another guy, I just need to make sure he respects the situation I described above.

I will leave some images below that illustrate what I am saying:

Alert na view para mostrar o valor vindo como 30000

Quando eu defino como int32 a máscara fica assim

Quando eu defino como decimal o alert mostra o retorno dessa forma

Quando eu defino como decimal o maskMoney acaba mostrando dessa forma

1 answer

1

Your question does not make much sense. First, because you are treating an entire as decimal, saving as decimal and yet wanting the field to behave as whole on screen. Obviously it won’t work right.

If the field will not have homes after the comma, only the use of decimal is already wrong, however much the field is in Reais, which, in theory, asks for the pennies. That’s why the field is called decimal: because has decimal places.

Here, for example, the conversion itself is for whole:

model.RoQuantidade = Convert.ToInt32(_sheet.Cells[lin, 3].value2);

If you pass 30000 to the Controller and the field is decimal, obviously the Mdelbinder will expect at least one decimal place (the normal is two). There is no problem with the mask-money: is the definition of variables that is incorrect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.