public ActionResult Create(string pid, string cmnt) { int p_id = Convert.ToInt32(pid); Comment comment = new Comment(); int u_id = (int) Session["u_id"]; comment.U_id = u_id; comment.State = 1; comment.P_id = p_id; comment.Description = cmnt; if (ModelState.IsValid) { ent.Comments.Add(comment); ent.SaveChanges(); } var errors = ModelState .Where(x => x.Value.Errors.Count > 0) .Select(x => new { x.Key, x.Value.Errors }) .ToArray(); CompanyProductUpload comments = new CompanyProductUpload { P_Comments = ent.Comments.Where(x=>x.P_id == p_id).ToList() }; return PartialView("~/Views/Products/_Comments.cshtml", comments); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CompanyProductUpload product = new CompanyProductUpload { ProductM = ent.Products.Find(id), }; if (product.ProductM == null) { return(HttpNotFound()); } product.BrandM = ent.Brands.SingleOrDefault(x => x.Id == product.ProductM.B_id); product.ProductModelM = ent.Models.SingleOrDefault(x => x.Id == product.ProductM.M_id); product.CategoryM = ent.Categories.SingleOrDefault(x => x.Id == product.BrandM.Cate_id); product.P_photos = ent.P_photo.Where(x => x.P_id == product.ProductM.Id).ToList(); if (Session["c_id"] == null) { TempData["Error"] = "You must log in to perfum this action"; return(RedirectToAction("Index", "Account")); } var s_id = (int)Session["c_id"]; if (s_id != product.ProductM.Cid) { TempData["Error"] = "You can only edit your own products !"; return(RedirectToAction("Index", "Products")); } return(View(product)); }
public ActionResult Delete(string id, string p_id) { int i_id = Convert.ToInt32(id); var comment = ent.Comments.Find(i_id); comment.State = 0; ent.Entry(comment).State = System.Data.Entity.EntityState.Modified; ent.SaveChanges(); int i_p_id = Convert.ToInt32(p_id); CompanyProductUpload comments = new CompanyProductUpload { P_Comments = ent.Comments.Where(x => x.P_id == i_p_id).ToList() }; return PartialView("~/Views/Products/_Comments.cshtml", comments); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } List <Category> temp = new List <Category>(); temp = GetCategorylist(); List <Brand> temp_br = new List <Brand>(); temp_br = GetBrandlist(); ProductsView pv = new ProductsView { Categories = temp, Product_b = temp_br }; CompanyProductUpload product = new CompanyProductUpload { ProductM = ent.Products.Find(id), Categories = pv }; if (product.ProductM == null) { return(HttpNotFound()); } product.P_Comments = ent.Comments.Where(x => x.P_id == product.ProductM.Id).ToList(); product.BrandM = ent.Brands.SingleOrDefault(x => x.Id == product.ProductM.B_id); product.CategoryM = ent.Categories.SingleOrDefault(x => x.Id == product.BrandM.Cate_id); product.ProductModelM = ent.Models.SingleOrDefault(x => x.Id == product.ProductM.M_id); product.P_photos = ent.P_photo.Where(x => x.P_id == product.ProductM.Id).ToList(); return(View(product)); }
public ActionResult Edit(CompanyProductUpload product, HttpPostedFileBase Photo) { P_photo _Photo = new P_photo(); if (Photo != null) { var length = Photo.InputStream.Length; MemoryStream target = new MemoryStream(); Photo.InputStream.CopyTo(target); _Photo.Photo = target.ToArray(); } if (ModelState.IsValid) { var cat_n = product.CategoryM.Name.Trim().ToLower(); // if there is category with that name if (!ent.Categories.Any(x => x.Name == cat_n)) { ent.Categories.Add(product.CategoryM); ent.SaveChanges(); product.BrandM.Cate_id = product.CategoryM.Id; } var brand_n = product.BrandM.Name.Trim().ToLower(); // same as up if (!ent.Brands.Any(x => x.Name == brand_n)) { ent.Brands.Add(product.BrandM); ent.SaveChanges(); product.ProductM.B_id = product.BrandM.Id; } var model_n = product.ProductModelM.Name.Trim().ToLower(); // same as up if (!ent.Models.Any(x => x.Name == model_n)) { ent.Models.Add(product.ProductModelM); ent.SaveChanges(); product.ProductM.M_id = product.ProductModelM.Id; } if (_Photo.Photo != null) { if (ent.P_photo.Any(x => x.P_id == product.ProductM.Id)) { var p = ent.P_photo.SingleOrDefault(x => x.P_id == product.ProductM.Id); p.Photo = _Photo.Photo; ent.Entry(p).State = EntityState.Modified; ent.SaveChanges(); } else { P_photo ph = new P_photo { P_id = product.ProductM.Id, Photo = _Photo.Photo }; ent.P_photo.Add(ph); ent.SaveChanges(); } } ent.Entry(product.ProductM).State = EntityState.Modified; ent.SaveChanges(); return(RedirectToAction("Profile", "Companies", new { id = (int)Session["c_id"] })); } return(View(product)); }
public ActionResult Create(CompanyProductUpload upload, HttpPostedFileBase Photo) { if (ModelState.IsValid) { Product product = new Product(); Brand brand = new Brand(); Category category = new Category(); Model model = new Model(); model.Name = upload.ProductModelM.Name.Trim().ToLower(); if (ent.Models.Any(x => x.Name == model.Name)) // if model with same name exist do not create new one { model = ent.Models.FirstOrDefault(x => x.Name == model.Name); } else { ent.Models.Add(model); ent.SaveChanges(); } category.Name = upload.CategoryM.Name.Trim().ToLower(); // same as model if (ent.Categories.Any(x => x.Name == category.Name)) { category = ent.Categories.FirstOrDefault(x => x.Name == category.Name); } else { ent.Categories.Add(category); ent.SaveChanges(); } brand.Name = upload.BrandM.Name.Trim().ToLower(); if (ent.Brands.Any(x => x.Name == brand.Name)) //same { brand = ent.Brands.FirstOrDefault(x => x.Name == brand.Name); } else { brand.Cate_id = category.Id; ent.Brands.Add(brand); ent.SaveChanges(); } product.Cid = (int)Session["c_id"]; product.Created_at = DateTime.UtcNow; product.Amount = upload.ProductM.Amount; product.Pname = upload.ProductM.Pname; product.Pdescription = upload.ProductM.Pdescription; product.B_id = brand.Id; product.M_id = model.Id; product.Price = upload.ProductM.Price; product.Status = 1; ent.Products.Add(product); ent.SaveChanges(); P_photo _Photo = new P_photo(); if (Photo != null) { var length = Photo.InputStream.Length; MemoryStream target = new MemoryStream(); Photo.InputStream.CopyTo(target); _Photo.Photo = target.ToArray(); _Photo.P_id = product.Id; ent.P_photo.Add(_Photo); ent.SaveChanges(); } return(RedirectToAction("Index")); } return(View(upload)); }