1
I am trying to set up a "simple" selling system, but I have no knowledge in e-commerce. What I want is the following:
The user chooses the products he wants, assembles a cart, and finalizes the order. There begins my question: the cart is half done, I can add the items and the desired amount, but I do not know how to calculate to display the total value.
Follows my Action:
public ActionResult AdicionarCarrinho(int id)
{
List<ItemDesign> carrinho = Session["Carrinho"] != null ? (List<ItemDesign>)Session["Carrinho"] : new List<ItemDesign>();
Design d = db.Design.Find(id);
if (d != null)
{
ItemDesign i = new ItemDesign();
i.Design = d;
i.Qtd = 1;
i.IdDesign = d.IdDesign;
if (carrinho.Where(x => x.IdDesign == d.IdDesign).FirstOrDefault() != null)
{
carrinho.Where(x => x.IdDesign == d.IdDesign).FirstOrDefault().Qtd += 1;
}
else
{
carrinho.Add(i);
}
Session["Carrinho"] = carrinho;
}
return RedirectToAction("Carrinho");
}
public ActionResult Carrinho()
{
List<ItemDesign> carrinho = Session["Carrinho"] != null ? (List<ItemDesign>)Session["Carrinho"] : new List<ItemDesign>();
return View(carrinho);
}
My first question is: How can I calculate the total value according to the items in the cart?
Second question: What to do after I finished the cart? What I was trying to do was more or less the following:
The user finalizes the order, and opens a payment screen (billet only). He would type in his data (name and Cpf, I don’t know) and the system would generate a "pseudo-billet" with the buyer’s data and the items he requested. It doesn’t have to be a real ticket, because I don’t intend to implement it.
Follow my tables:
Design:
IdDesign
Servico
Preco
ItemDesign:
Id
IdDesign
IdPedido
Pedido:
IdPedido
DtPedido
StatusPedido
IdUsuario
DtPagamento
ValorTotal
I accept suggestion of any tutorial/book on E-commerce.
Edit: View trolley
@model List<AllFiction.Models.ItemDesign>
@{
ViewBag.Title = "Carrinho";
}
<h2>Carrinho</h2>
@foreach (var item in Model)
{
<li>
@item.IdDesign - @item.Design.Servico
<input type="text" value="@item.Qtd" readonly="readonly"/>
<input type="submit" value="alterar" />
@Html.ActionLink("Remover", "Remover", "Shop", new {id=item.IdDesign}, null)
</li>
}
@Html.ActionLink("Retornar", "Servicos", "Shop")
Do you want to do this calculation on View or in Controller?
– Leonel Sanches da Silva
I think in the controller. I intend to save this value later in the database, or no difference?
– Ryan Santos
In this case, it is better in the Controller even.
– Leonel Sanches da Silva