Monetary values with Asp.net MVC

Asked

Viewed 1,630 times

4

I am facing difficulty in working with monetary values (decimals) using web application, asp.net MVC, with database Mysql. The problem is I can’t edit decimal values, like: 53.50, or 53.50. I can’t save using a semicolon. The problem is also that web.config is already set up globalization with en.

Follows 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>
  • According to the question that was compared to duplicated, the solution of it unfortunately does not cover my question.

  • 1

    We’ll reopen, then I’ll answer to you.

1 answer

2


Install the Nuget package in your application jQuery Money Mask.

See examples of usage and configuration 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" }})

Browser other questions tagged

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