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?
– Renan
What technology are you using to save the data? Put the sources, so we can try to help you.
– Tiedt Tech
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 !
– Érik Thiago
Have you tried
Session["Carrinho"] = null
? If the problem is cleaning the cart just set theSession
for nil.– Diego Zanardo
cart.ItemPedido.Clear()
– Rod