Creation of a Shopping Cart

Asked

Viewed 807 times

0

I want to create a Cart, where you have the Products that I will sell, the quantity and the total value. Does anyone have an idea of how I can start development?

Just for clarification, follow my class of Product, which starts from a Menu:

public class Produto
{
    [Key]
    public int ProdutoID { get; set; }

    [Required(ErrorMessage = "Preencha o nome do produto")]
    [DisplayName("Produto")]
    [StringLength(20, MinimumLength = 5, ErrorMessage = "O nome do produto deve ter entre 5 a 20 caracteres")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "Preencha a descrição do produto")]
    [DisplayName("Descrição")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "O nome do produto deve ter entre 5 a 50 caracteres")]
    public string Descricao { get; set; }

    [DisplayName("Foto")]
    public string Foto { get; set; }

    [Required(ErrorMessage = "Preencha o valor do produto")]
    [DisplayName("Valor R$")]
    public decimal Valor { get; set; }

    //relacionamentos
    public int RestauranteID { get; set; }
    public virtual Restaurante Restaurante { get; set; }
}
  • I already prefer to store in the browser localstorage

1 answer

3


Dude, in MVC I create temporary data that is saved in the session. Let me give you an example of a system I made for a haburgueria taking into account your Model:

Data Temporales

Here you will store the order data in the session. Let’s assume that an order is a list of products that the user has chosen

using System.Web;
using Hamburgueria.Models;
using System.Collections.Generic;

namespace Hamburgueria
{
    public class DadosTemporarios
    {
        const string CHAVE_PEDIDOS = "PEDIDOS";

    internal static void ArmazenaPedidos(Produto produtoDoPedido)
    {
        List<Produto> pedidos = RetornaPedidos() != null ?
            Retornapedidos() : new List<Produto>();

        pedidos.Add(produtoDoPedido);
        HttpContext.Current.Session[CHAVE_PEDIDOS] = pedidos;
    }

    internal static void RemovePedidos(int id)
    {
        List<Produto> pedidos = RetornaPedidos();
        pedidos.RemoveAt(id);
        HttpContext.Current.Session[CHAVE_PEDIDOS] = pedidos;
    }

    internal static void RemovePedidos()
    {
        HttpContext.Current.Session[CHAVE_PEDIDOS] = new List<Produto>();
    }

    internal static List<Produto> RetornaPedidos()
    {
        return HttpContext.Current.Session[CHAVE_PEDIDOS] != null ?
            (List<Produto>)HttpContext.Current.Session[CHAVE_PEDIDOS] : null;
    }
}
 }

Right, but now what? Simple! in your Controller you just need to use the methods of this class to save in session, like this:

public ActionResult AdicionarProdutoAoCarrinho(Produto produto)
        {
            if(ModelState.IsValid)
                DadosTemporarios.ArmazenaPedidos(produto);
            else{
                ///TODO
            }

            return View("Index", produtos);
        }

In your view you can still call the method that returns the items in the cart to show how many still have :)

In the case of this my project I went a little further with the database. In my case it is just a restaurant but you can change it easy. Follow the pattern

inserir a descrição da imagem aqui

Just follow this path that you get easy :) Thanks

  • In this case, would the Products be in an Order Index? A list, in this case?

  • What would that be: const string CHAVE_PEDIDOS = "REQUESTS"; ?

  • Yes. If you are going to use Fist database just do a normal scaffold and create all CRUD just by right clicking. It already creates the views as well. Ahh, at the end of the Insert (when the client finishes the order) clear the session data with "Session.Clear();" This Const string is a key that I create and use in each Session of my current context. If I wanted to save two different data (e.g. orders and currently logged in user) I would need another key

  • In this case, the Customer in my program (logged in through a Session) will make the purchase. What should I put in there?

  • puts anything like key, man... then just follow the example I gave to make the storage of that user. Mark the answer as right, namizade :)

  • In fact, it was very vague. Taking into account that I will use an order table still, in addition to the product table. And I still need to click the product to get it on the order list.

  • How did it get vacant? your question was "how can I start development" and I gave you a database and the whole scheme to save in the session in a good way. you want me to develop so you won’t learn anything

  • But how will I add the items in the Cart through a button, inside the menu, for example?

  • Dude, then you have to create a form and create an input button that will receive the post. there you do as I said in the reply: if(ModelState.IsValid)&#xA; DadosTemporarios.ArmazenaPedidos(produto);

Show 4 more comments

Browser other questions tagged

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