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

2

I have a model with the following definition:

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

In my View to fill in the data when empty, that is, that have not yet been written, using maskMoney works without problems. I use the following form:

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

Please note that the accuracy is zero because I do not need decimal places in this case.

The problem starts when I 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 getting information from an Excel spreadsheet, but that's not the problem. It turns out that the return to the view same as removing the mask or trying to convert, always return as follows:

  • Original value: 30000
  • Value returned to view : 30000,00

If I use maskMoney the way I put it up 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 displays the value: 3,000,000, because the field being decimal added the two zeroes transforming from 30 thousand to 3 million

I tried to change the definition of the field to int32 , however the formatting is in trouble and at the time of recording it informs that there is an error because it can not write 30,000 (notice that it changed even with the correct definition of the point by the comma) .

I have tried to use other jquery plugins like maskedit, but I was not able to do it, especially when the information is returned.

If you have any idea how I can do to fill the formatted value, save the information without formatting, recover the information without formatting and present in a formatted form, there is no problem changing from decimal to another type, only I need to make him respect the situation I described above.

I'll leave some images below that illustrate what I'm saying:

    
asked by anonymous 08.04.2016 / 19:19

1 answer

1

Your question does not make much sense. First, because you're treating an integer as decimal , saving as decimal and still wanting the field to behave as integer on screen. Obviously it will not work properly.

If the field will have no houses after the comma, only the use of decimal is already wrong, even though the field is in Reais , which in theory asks for the cents . This is why the field is called decimal : because has decimal places .

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

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

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

    
14.09.2016 / 19:36