Answer the question
In order to leave comments, you need to log in
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; }
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question