-1
I have a table called Pecas where only has name and value, but when I click create new piece is not saving in the database, appears the msg that was added but is not entering, in debug shows the name but the value is always getting 0 no matter the value I put.
Table
public class Pecas
{
public int Id { get; set; }
public string Nome { get; set; }
public decimal ValorUnitatio { get; set; }
}
Tablet
public PecasVM()
{
}
public PecasVM(Pecas row)
{
Id = row.Id;
Nome = row.Nome;
ValorUnitatio = row.ValorUnitatio;
}
public int Id { get; set; }
public string Nome { get; set; }
public decimal ValorUnitatio { get; set; }
My controller
public ActionResult Criar()
{
return View();
}
[HttpPost]
public ActionResult Criar(PecasVM model)
{
if (ModelState.IsValid)
{
using (Db db = new Db())
{
Pecas pec = new Pecas();
pec.Nome = model.Nome;
pec.ValorUnitatio = model.ValorUnitatio;
db.Pecas.Add(pec);
db.SaveChanges();
}
}
TempData["MSG"] = "Peça adicionada com sucesso.";
return RedirectToAction("Index");
}
Some doubts: when you say value you are referring to
ValorUnitario
? If it is, it cannot be a conversion error since the value isdecimal
? Are you passing an integer value, no comma/dot? Still with zero value inValorUnitario
should enter normally because zero is an acceptable value.Id
, I imagine he’sidentity/sequence/auto_increment
etc in your right bank?– Ricardo Pontual
Probably the json that arrives is coming as a string, since it cannot convert, it uses the default value of the decimal that is zero.
– Gabriel Coletta
Have you tried to map the property as
double
?– Leandro Angelo
He decided to work alone, without me changing anything, tried with a comma and a period and was not going and now this inserting.
– Cezar Mdlosci
certainly ... if (Modelstate.Isvalid) if it is invalid will show that entered
– Marco Souza
@As Cezarmdlosci solved "alone", it would not be better to close the question?
– Leandro Angelo