Save Request - ASP.NET MVC

Asked

Viewed 1,435 times

1

I want to make a simple Ecommerce on my website based on this question: Implementation of ASP.NET MVC Shopping Cart

However, I am doubtful when it comes to doing two things: 1º Save the data in the Order and Itempedido table, with some data coming from the cart Session. 2º How to remove items from cart? To add it is all right, but I am unable to remove.

Like I said, I’m doing according to the link above, so my code is pretty much the same. Follow them:

public ActionResult AdicionarCarrinho(int id)
{

    Pedido carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();

    var produto= db.Produto.Find(id);

    if produto!= null)
    {
        var itemPedido= new ItemPedido();
        itemPedido.Produto = produto;
        itemPedido.Qtd = 1;

        if (carrinho.ItemPedido.FirstOrDefault(p => p.IdProduto== produto.IdProduto) != null)
        {
            carrinho.ItemPedido.FirstOrDefault(p => p.IdProduto== produto.IdProduto).Qtd += 1;
        }

        else
        {
            carrinho.ItemPedido.Add(itemPedido);
        }



        carrinho.ValorTotal = carrinho.ItemPedido.Select(i => i.Produto).Sum(d => d.Preco);

        Session["Carrinho"] = carrinho;
    }

    return RedirectToAction("Carrinho");
}

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

    return View(carrinho);
}

And the tables in the database:

Produto:
IdProduto
Nome
Preco

ItemPedido:
Id
IdProduto
IdPedido

Pedido:
IdPedido
DtPedido
StatusPedido
IdUsuario
DtPagamento
Valor
  • Show your code, how are you trying to do it? Is there an error? If so, enter the error in your question to try to help. The title is "Save Request - ASP.NET MVC" but your question is about how to remove?

  • What technology are you using to save the data? Put the sources, so we can try to help you.

  • Well William, since you are new to the language and want to learn, first go to the official website of Asp.Net to learn more about the language. As the tutorial that I will pass here is very big I will pass the link here and not by response and you learn. In short: It is a E-comerce project that even has shopping cart. So take a look and learn !

  • Have you tried Session["Carrinho"] = null ? If the problem is cleaning the cart just set the Session for nil.

  • cart.ItemPedido.Clear()

1 answer

2


I’m guessing, by displaying your cart, ItemPedido already have an ID filled by you. That is, there would be a change here:

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

    var produto= db.Produto.Find(id);

    if produto!= null)
    {
        var itemPedido= new ItemPedido();
        itemPedido.Id = // Coloque aqui um Id
        itemPedido.Produto = produto;
        itemPedido.Qtd = 1;
    }

    ...
}

Only, I don’t recommend using whole Ids in this part. Ideally, a generated Id (such as a Guid, for example. Microsoft has this structure that the use is transparent in ASP.NET MVC.

I mean, your Model would look like this:

public class ItemPedido 
{
    [Key]
    public Guid ItemPedidoId { get; set; }
    public Guid ProdutoId { get; set; }
    public Guid PedidoId { get; set; }

    public virtual Produto Produto { get; set; }
    public virtual Pedido Pedido { get; set; }
}

And the method in Controller:

public ActionResult AdicionarCarrinho(int id)
{

    Pedido carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();

    var produto= db.Produto.Find(id);

    if produto!= null)
    {
        var itemPedido= new ItemPedido();
        itemPedido.Id = Guid.NewGuid();
        itemPedido.Produto = produto;
        itemPedido.Qtd = 1;
    }

    ...
}

To delete now becomes simpler. Create one more Action in the Controller:

public ActionResult ExcluirItem(Guid id) 
{
    var carrinho = Session["Carrinho"] != null ? (Pedido)Session["Carrinho"] : new Pedido();
    var itemExclusao = carrinho.ItemPedido.FirstOrDefault(i => i.ItemPedidoId == id);
    carrinho.ItemPedido.Remove(itemExclusao);

    Session["Carrinho"] = carrinho;
    return RedirectToAction("Carrinho");
}

To save (I am assuming at least Entity Framework 6 for this logic to work):

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

    contexto.Pedidos.Add(carrinho);
    contexto.SaveChanges();

    return RedirectToAction("Carrinho");
}
  • when I click finish cart gives the following error: "An Entity Object cannot be referenced by multiples instances of Ientitychangetracker". and a second question when I save the request, automatically I saved in the table itempedido tbm? as I need to display the order along with the requested item data.

  • If you did as in the reply, and it is Entity Framework 6, the context saves the request and its respective items. As to the problem, this is the scope of a specific question.

  • po guy wants me to open another question related to this?

  • Yes, please.

  • I’ve already opened a new question.

Browser other questions tagged

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