jQuery Mask Plugin does not format the number correctly

0

I'm working on an MVC4 project, the mask works correctly when saved, but when returning the database data the plugin shows incorrect data if the value ends at 0.

Data example: 99.000,00 , when I go in the Edit View it gets: 990,00

I can do a trick to fix this, but since I'm using MVC4, I'd like to know how to handle this situation.

View:

    <div class="campo">
        @Html.LabelFor(model => model.ValorPadrao)
        <br />
        @Html.TextBoxFor(model => model.ValorPadrao, new { style = "width:400px", @class ="maskMoeda" })
    </div>

Call Javascript:

$('.maskMoeda').mask('999.999.999.999.999,00', { reverse: true })
    .css('text-align', 'right');

Field in Model:

public decimal? ValorPadrao { get; set; }
    
asked by anonymous 18.07.2014 / 21:03

2 answers

1

I found the solution! I added the StringFormat in TextBoxFor :

<div class="campo">
        @Html.LabelFor(model => model.ValorPadrao)
        <br />
        @Html.TextBoxFor(model => model.ValorPadrao, "{0:F2}", new { style = "width:400px", @class="maskMoeda" })
</div>

And I've added these two annotations to Model :

[DisplayFormat(DataFormatString = "{0:0,0.00}", ApplyFormatInEditMode = true)]
[DataType(DataType.Currency)]
public decimal? ValorUnitario { get; set; }
    
21.07.2014 / 14:57
0

There's a question of mine in which I try to put this mask as an attribute of the Model , but to this day I did not get the ideal solution. This answer is the one that comes closest to solving the problem in the most performative way possible, but it is not yet 100% . Overall, it's a good way to resolve.

Another way, a bit easier and simpler to do is to put your code inside @section scripts , at the end of your View code:

@section scripts {
    <script type="text/javascript">
        $(document).ready(function() {
            $('.maskMoeda').mask('999.999.999.999.999,00', { reverse: true })
                .css('text-align', 'right');
        });
    </script>
}

Very important detail: This solution does not work on Partial Views . Partial Views do not support% s of% s. If you really want to use this solution, put @section in the parent view.

    
18.07.2014 / 23:24