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).