Field of decimal type Entity Framework ASP.NET MVC

0

I have a table named Pecas where only has name and value, but when I click on create new piece is not saving in the database, the msg that was added but not inserting, the debug shows the name but the value always it's coming 0 no matter what the value I put.

Table

    public class Pecas
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public decimal ValorUnitatio { get; set; }


}

TableVM

        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");
    }
    
asked by anonymous 30.01.2018 / 02:09

1 answer

0

I solved it as follows:

        public ActionResult Detalhes(int id)
    {


        ConsertoVM model;
        using (Db db= new Db())
        {

            decimal total = 0m;

            Conserto cons = db.Conserto.Find(id);
            decimal vPeca = cons.Pecas.ValorUnitatio;
            decimal maoObra = cons.ValorMaoObra;

            total += vPeca + maoObra;
            cons.ValorTotalConserto = total;

            model = new ConsertoVM(cons);
        }
        return View(model);
    }

Of course it could be done in a different way but I wanted to do so.

    
31.01.2018 / 23:52