Monetary values with asp.net MVC

4

I am having difficulty working with monetary values (decimals), using web application, asp.net MVC , with database Mysql . The problem is that I can not edit decimal values, such as: 53.50 , or 53.50 . I can not save using dot, not comma. The problem is also that web.config is already configured globalization with en .

Follow the model:

[DisplayName("Preço:")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:C}")]
public Nullable<decimal> preco { get; set; }

Follow the view:

<div class="form-group">
     @Html.LabelFor(model => model.preco, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10">
     @Html.EditorFor(model => model.preco, new { htmlAttributes = new { @class = "form-control", @placeholder = "somente numeros" } })
     @Html.ValidationMessageFor(model => model.preco, "", new { @class = "text-danger" })
     </div>
</div>

Web.config:

<system.web>
    <globalization culture="pt-BR" uiCulture="pt-BR" />
    ...
</system.web>
    
asked by anonymous 09.05.2015 / 05:33

1 answer

2

Install the NuGet package jQuery Money Mask into your application.

See usage and configuration examples here .

Avoid using @Html.EditorFor() . It does not allow mask or class addition because it is defined as a template . Switch to:

@Html.TextBoxFor(model => model.preco, new { @class = "form-control", @placeholder = "somente números" })

Still, if you want to use @Html.EditorFor() . works as follows:

@Html.EditorFor(model => model.preco, new { htmlAttributes = new { @class = "form-control", @placeholder = "somente números" }})
    
10.05.2015 / 23:48