Taking data from the Entity framework database (picking up discount coupons)

Asked

Viewed 48 times

0

Good evening, I wonder how I can do by ajax to it go through my bank and see and there is a discount apply code:

DAO:

public void Adiciona(Desconto desconto)
    {
        using (var context = new LojaContext())
        {
            context.Descontos.Add(desconto);
            context.SaveChanges();
        }
    }

    public IList<Desconto> Lista()
    {
        using (var contexto = new LojaContext())
        {
            return contexto.Descontos.ToList();
        }
    }

    public void Atualiza(Desconto desconto)
    {
        using (var contexto = new LojaContext())
        {
            contexto.Descontos.Update(desconto);
            contexto.SaveChanges();
        }
    }
    public Desconto BuscaPorId(int id)
    {
        using (var contexto = new LojaContext())
        {
            return contexto.Descontos.Find(id);
        }
    }

My table in the database shows the ID, DISCOUNT PERCENTAGE AND DISCOUNT CODE.

My trolley controller where you have the button to put the code and apply the discount:

public ActionResult AdicionarCarrinho(int id)
    {
        var carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();


        var produto = new ProdutosDAO().BuscaPorId(id);

        foreach (var item in carrinho.ItensPedido)
        {
            if (item.Produto.Id == produto.Id)
            {
                item.Quantidade++;
                Session["Carrinho"] = carrinho;
                return RedirectToAction("Carrinho");
            }
        }
        carrinho.AdicionaProduto(produto);
        Session["Carrinho"] = carrinho;
        return RedirectToAction("Carrinho");
    }
    public ActionResult ExcluiProdutoCarrinho(int id)
    {
        var carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();
        var produto = new ProdutosDAO().BuscaPorId(id);
        carrinho.RemoverProduto(produto.Id);
        Session["Carrinho"] = carrinho;
        return RedirectToAction("Carrinho");
    }
    public ActionResult Carrinho()
    {
        Pedido carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();
        var produtos = carrinho.ItensPedido;
        ViewBag.Produtos = produtos;
        return View(carrinho);
    }

    //public ActionResult AdicionarDesconto(int id)
    //{
    //    if ()
    //}

My button: Discount

I need to know how to add these discount coupons and make an ajax to pick them up at the bank and use at the discount button.

  • But how is the logic in the application? In the view the user who enters with a discount code? Or will this be checked in some other action? The way you explained it and without your view we won’t be able to help much.

  • Put the code you started to make the call ajax based on what you have already searched and tried.

1 answer

0

My ajax

    function aplicaDesconto() {
        var codigo = $("#codDesconto").val();
        var total = @total;
        $.ajax({
            type: 'POST',
            url: 'AplicaDesconto',
            data: {
                codigo
            },
            success: function (data) {
                if (data.sucesso == true) {
                    $(".totalCarrinhoCompra").text(@total  * 0.20);

                    var DESCONTO10 = @total  * 0.10;
                    var DESCONTO20 = @total  * 0.20;
                    var DESCONTO30 = @total  * 0.30;
                    var DESCONTO40 = @total  * 0.40;
                    var DESCONTO50 = @total  * 0.50;
                    var DESCONTO60 = @total  * 0.60;
                    var DESCONTO70 = @total  * 0.70;
                    var DESCONTO80 = @total  * 0.80;
                    var DESCONTO90 = @total  * 0.90;


                } else {

                }

            }

        })
    }

My button:

// Discount // >

Not taking the items and applying the discount..

Errors: Uncaught Syntaxerror: Unexpected number

Uncaught Referenceerror: applicationDesconto is not defined At Htmlbuttonelement.onclick

Browser other questions tagged

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