jQuery Mask Plugin does not format the number properly

Asked

Viewed 1,799 times

0

I am working on a MVC4 project, the mask works correctly when saved, but when returning the database data the plugin shows incorrect data if the value ends in 0.

Data example: 99.000,00, when I go to the Edit View: 990,00

I can do a gambiarra to fix this, but as I am using MVC4, I would like to know the correct way to deal with this situation.

View:

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

Javascript call:

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

Field in the Model:

public decimal? ValorPadrao { get; set; }
  • Because it does not ascribe to the input the value without formatting (double), in which case it would be "99000.00". I believe that in the mask passage it should format correctly.

  • I don’t understand, you can explain better?

  • Don’t bag well of . Net, but wouldn’t have been able to assign the value without formatting to the input in the page rendering? In the case with "." as decimal separator and without "," in the thousand separator.

2 answers

1


I found the solution! I added Stringformat, in the 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 added these two notes to Model:

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

0

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

Another way, a little easier and simple to do (on the other hand, an answer that can make your code become repetitive) is by putting your code inside the @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 in Partial Views. Partial Views do not admit @sections. If you really want to use this solution, put the @section in Father View.

  • Gypsy, I managed to make it work without having to do this, I managed to pass Stringformat in Textboxfor, and added a property in Model. Monday I put the solution here, maybe it’ll work for you too.

  • @NULL I don’t think so. In mine I want to write an attribute on top of the property in the Model (for example, [CNPJ]) and the on-screen field already has the validation mask without having to touch Stringformat. But thank you ;)

Browser other questions tagged

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