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)); }