Problem with decimals in ASP.NET MVC 5

0

I have a payment administration in which I have an error that I have not been able to solve so far. The error is given in the monetary value of the payment because the value that is entered in the view generates a decimal number of type xxxx.00. That is, if I type the value 5.60, the value 560.00 returns, if I put 40.60, it returns 4060.00 etc.

The code I use is the following

Model

[Column, Display(Name = "Valor"), Required, DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
public decimal valor { get; set; }

View

<div class="form-group">
    @Html.LabelFor(model => model.valor, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.valor, new { htmlAttributes = new { @class = "form-control", @type = "number", @step = "0.01"} })
        @Html.ValidationMessageFor(model => model.valor, "", new { @class = "text-danger" })
    </div>
</div>

Controller

public ActionResult Create(FormCollection collection)
    {
        try
        {
            Proyecto proy = r.proyectos.Single(i => i.id == Global.idProyectoActual);
            Pago pago = new Pago();
            pago.valor = Convert.ToDecimal(collection["valor"].ToString());
            pago.factura = collection["factura"];
            pago.idProyecto = Global.idProyectoActual;
            pago.fechaRegistro = DateTime.Now;
            r.pagos.InsertOnSubmit(pago);

            proy.valorRestante = proy.valorRestante - pago.valor;

            r.SubmitChanges();
            return Redirect("~/Proyecto/Details/"+Global.idProyectoActual);
        }
        catch
        {
            return View();
        }
    }

It is necessary to clarify that the error arises in the post ActionResult method only, because if I directly load in the base a value for example 20.10 that data appears if no problem in the tables of the views (generally Index) as well as in the view of modification (usually Edit).

    
asked by Kevtho 15.02.2018 в 02:25
source

2 answers

0

With the {0:F2} format you are forcing a decimal point to be placed and at the end double zero, that is why you are shown those results.

To correct it you will have to change it by {0:C} or in your case, use {C} , in this way, you will respect the format and put it as currency format. For example, for the value 40.60, you will get $40.60 . The annotation would finally be:

[Column, Display(Name = "Valor"), Required, DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
public decimal valor { get; set; }

Here you can see the demo and the result.

    
answered by 15.02.2018 в 02:55
0

I am doing tests and I see that the Action reaches the correct value. The "," are enabled to enter the input so it is not format. In fact I'm simulating the parse you use in this line:

Convert.ToDecimal(collection["valor"].ToString()); 

and I did not have problems either. Moreover, this parsing is unnecessary since it reaches the Action with the correct data type (decimal). For the POST I used:

$.ajax({
        type: "POST",
        url: "/Home/Create",
        data: $("form").serialize()
    });

In the view the only thing you added was a tag form that includes your view and an onSubmit event that calls the ajax that I detailed above. Maybe it's the POST you're doing ... how are you doing?

    
answered by 16.02.2018 в 20:30