コード例 #1
0
        public ActionResult Delete(int id, Product product)
        {
            try
            {
                List <Image> images = services.DeleteImagesFromProduct(id);
                foreach (var item in images)
                {
                    string filePath = Server.MapPath(item.Url);
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                }

                services.DeleteProduct(id);

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
コード例 #2
0
        public ActionResult DeleteConfirmed(string id)
        {
            var user         = UserManager.FindById(id);
            var logins       = user.Logins;
            var rolesForUser = UserManager.GetRoles(id);


            foreach (var login in logins.ToList())
            {
                var t = UserManager.RemoveLogin(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
            }

            if (rolesForUser.Count() > 0)
            {
                foreach (var item in rolesForUser.ToList())
                {
                    // item should be the name of the role
                    var result = UserManager.RemoveFromRole(user.Id, item);
                }
            }
            Supplier supplier = context.suppliers.FirstOrDefault(c => c.supID == user.Id);
            Customer customer = context.customers.FirstOrDefault(c => c.CusID == user.Id);
            services services = new services();

            if (supplier != null)
            {
                List <Product> products = context.products.Where(c => c.supplierID == supplier.supID).ToList();
                foreach (var item in products)
                {
                    services.DeleteProduct(item.ID);
                }
                context.suppliers.Remove(supplier);
                context.SaveChanges();
            }
            if (customer != null)
            {
                List <Comment> comments = context.comments.Where(c => c.CustomerId == customer.CusID).ToList();
                foreach (var item in comments)
                {
                    context.comments.Remove(item);
                    context.SaveChanges();
                }
                List <Order> orders = context.orders.Where(c => c.CustomerId == customer.CusID).ToList();
                foreach (var item in orders)
                {
                    List <OrderDetails> orderDetails = context.OrderDetails.Where(c => c.OrderId == item.Id).ToList();
                    foreach (var i in orderDetails)
                    {
                        context.OrderDetails.Remove(i);
                        context.SaveChanges();
                    }
                    context.orders.Remove(item);
                    context.SaveChanges();
                }
                context.customers.Remove(customer);
                context.SaveChanges();
            }
            //Delete User

            UserManager.Delete(user);



            return(RedirectToAction("Index"));
        }