public List<Phanhoi> ThemPhanHoi(int BinhLuanId,string NoiDung){
     var dbContext = new shopContext();
     var userName = HttpContext.Session.GetString("username");
     var taiKhoan = (from tk in dbContext.Taikhoan where tk.Username == userName select tk).ToList();
     var phanHoi = new Phanhoi(){
         BinhLuanId = BinhLuanId,
         TaiKhoanId = taiKhoan[0].TaiKhoanId,
         NoiDung = NoiDung
     };
     dbContext.Phanhoi.Add(phanHoi);
     dbContext.SaveChanges();
     var DSPhanHoi = (from ph in dbContext.Phanhoi
                         join tk in dbContext.Taikhoan
                         on ph.TaiKhoanId equals tk.TaiKhoanId
                         where ph.BinhLuanId == BinhLuanId
                         orderby ph.PhanHoiId descending
                         select new {
                             NoiDung = ph.NoiDung,
                             userName = tk.Username
                         }).ToList();
     List<Phanhoi> ListPhanHoi = new List<Phanhoi>();
     foreach (var item in DSPhanHoi)
     {
         Phanhoi ph = new Phanhoi();
         ph.BinhLuanId = BinhLuanId;
         ph.NoiDung = item.NoiDung;
         Taikhoan tk = new Taikhoan();
         tk.Username = item.userName;
         ph.TaiKhoan = tk;
         ListPhanHoi.Add(ph);
     }
     dbContext.SaveChanges();
     return ListPhanHoi;
 }
Exemple #2
0
        public IActionResult ChiTietSanPham(int id)
        {
            var dbContext = new shopContext();
            var SanPham   = (from sp in dbContext.Sanpham
                             join h in dbContext.Hinhanh
                             on sp.HinhAnhId equals h.HinhAnhId
                             where sp.SanPhamId == id
                             select new
            {
                SanPhamId = sp.SanPhamId,
                TenSanPham = sp.TenSanPham,
                Mota = sp.Mota,
                GiaBanLe = sp.GiaBanLe,
                TenFile = h.TenFile
            });

            List <Sanpham> List = new List <Sanpham>();

            foreach (var sp in SanPham)
            {
                Sanpham sanPham = new Sanpham();
                Hinhanh h       = new Hinhanh();
                sanPham.SanPhamId  = sp.SanPhamId;
                sanPham.TenSanPham = sp.TenSanPham;
                sanPham.Mota       = sp.Mota;
                sanPham.GiaBanLe   = sp.GiaBanLe;
                h.TenFile          = sp.TenFile;
                sanPham.HinhAnh    = h;
                List.Add(sanPham);
            }
            ViewData["sanpham"] = List;

            // hiển thị danh sách bình luận
            var binhLuan = (from bl in dbContext.Binhluan
                            join tk in dbContext.Taikhoan
                            on bl.TaiKhoanId equals tk.TaiKhoanId

                            where bl.SanPhamId == id
                            select new {
                binhluanId = bl.BinhLuanId,
                noidung = bl.NoiDung,
                username = tk.Username
            }).ToList();
            List <Binhluan> DSBinhLuan = new List <Binhluan>();

            foreach (var item in binhLuan)
            {
                Binhluan bl = new Binhluan();
                Taikhoan tk = new Taikhoan();
                bl.BinhLuanId = item.binhluanId;
                bl.NoiDung    = item.noidung;
                string Str1 = item.username.Substring(0, 1);
                tk.Username = Str1;
                tk.Password = item.username;
                bl.TaiKhoan = tk;

                DSBinhLuan.Add(bl);
            }

            if (DSBinhLuan.Count == 0)
            {
                ViewBag.empty = "Không có bình luận cho sản phẩm này";
            }
            ViewBag.binhluan = DSBinhLuan;
            //hien thi danh sach phan hoi
            var DSPhanHoi = (from ph in dbContext.Phanhoi
                             join tk in dbContext.Taikhoan
                             on ph.TaiKhoanId equals tk.TaiKhoanId
                             orderby ph.PhanHoiId descending
                             select new {
                PhanHoiId = ph.PhanHoiId,
                BinhLuanId = ph.BinhLuanId,
                NoiDung = ph.NoiDung,
                userName = tk.Username
            }).ToList();
            List <Phanhoi> ListPhanHoi = new List <Phanhoi>();

            foreach (var item in DSPhanHoi)
            {
                Phanhoi ph = new Phanhoi();
                ph.PhanHoiId  = item.PhanHoiId;
                ph.BinhLuanId = item.BinhLuanId;
                ph.NoiDung    = item.NoiDung;
                Taikhoan tk   = new Taikhoan();
                string   Str1 = item.userName.Substring(0, 1);
                tk.Password = Str1;
                tk.Username = item.userName;
                ph.TaiKhoan = tk;
                ListPhanHoi.Add(ph);
            }
            ViewBag.phanhoi = ListPhanHoi;
            return(View());
        }