A
A
Alexxxey_enot2021-03-16 16:01:09
ASP.NET
Alexxxey_enot, 2021-03-16 16:01:09

Can't call a class in a method and assign data from this class?

PS Implementing ASP.NET Core add to cart feature
Error is here

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WA12.DB;

namespace WA12.Models
{
    public class ShopCart 
    {
        private readonly AppDBContent appDBContent;
        public ShopCart(AppDBContent appDBContent)
        {
            this.appDBContent = appDBContent;
        }
        public string ShopCartId { get; set; }

        public List<ShopCartItem> listShopItems { get; set; }

        public static ShopCart GetCart(IServiceProvider services)
        {
            ISession session = services.GetRequiredService<IHttpContextAccessor>()?.HttpContext.Session;
            var context = services.GetService<AppDBContent>();
            string shopCartId = session.GetString("CartId") ?? Guid.NewGuid().ToString();

            session.SetString("CartId", shopCartId);

            return new ShopCart(context) { ShopCartId = shopCartId };      
        }
        public void AddToCart(Realty realty, int amount)
        {
            appDBContent.ShopCartItem.Add(new ShopCartItem
            { 
                ShopCartId = ShopCartId,
                realty = realty

            });
            appDBContent.SaveChanges();

        }

       
    }
}


public class ShopCartItem
    {
        public int id { get; set; }
        public Realty realty { get; set; }
        public int price { get; set; }

        public string ShopCartId { get; set; }


    }


public class AppDBContent : DbContext
    {
        public AppDBContent(DbContextOptions<AppDBContent> options) : base(options)
        {

        }
        public DbSet<Realty> Realty { get; set; }
        public DbSet<Category> Category { get; set; }
        public DbSet<ShopCartItem> CartItem { get; set; }
    }


6050ad79e1e7c900470661.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-03-16
@Alexxxey_enot

You don't have AppDBContentany field ShopCartItem. Take a closer look at your own code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question