Exemple #1
0
        public ActionResult Insert(Post item, List <HttpPostedFileBase> fluResim)
        {
            {
                ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", item.CategoryID);

                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        item.UserID      = Convert.ToInt32(Session["id"]);
                        item.PublishDate = DateTime.Now;
                        item.IsDeleted   = false;

                        if (ModelState.IsValid)
                        {
                            db.Posts.Add(item);
                            bool sonuc = db.SaveChanges() > 0;
                            if (sonuc)
                            {
                                foreach (HttpPostedFileBase img in fluResim)
                                {
                                    PostImage pi = new PostImage();
                                    pi.PostID = item.PostID;
                                    bool   yuklemeSonucu;
                                    string path = FxFonksiyon.ImageUpload(img, "posts", out yuklemeSonucu);
                                    if (yuklemeSonucu)
                                    {
                                        pi.ImagePath = path;
                                    }
                                    db.PostImages.Add(pi);
                                    db.SaveChanges();
                                }
                                ts.Complete();
                                TempData["Message"] = FxFonksiyon.GetInformation(MessageFormat.OK);
                                return(RedirectToAction("Index", "Post"));
                            }
                            else
                            {
                                TempData["Message"] = FxFonksiyon.GetInformation(MessageFormat.Err);
                            }
                        }
                        else
                        {
                            TempData["Message"] = FxFonksiyon.GetInformation(MessageFormat.Val);
                        }
                    }
                }
                catch (Exception)
                {
                    TempData["Message"] = FxFonksiyon.GetInformation(MessageFormat.Err);
                }
                return(View());
            }
        }
        public ActionResult Edit(Product item, List <HttpPostedFileBase> fluResim)
        {
            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", item.CategoryID);
            ViewBag.BrandID    = new SelectList(db.Brands, "BrandID", "BrandName", item.BrandID);
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    if (ModelState.IsValid)
                    {
                        db.Entry(db.Products.Find(item.ProductID)).CurrentValues.SetValues(item);
                        bool sonuc = db.SaveChanges() > 0;
                        if (sonuc)
                        {
                            if (fluResim.Count > 0)
                            {
                                foreach (HttpPostedFileBase img in fluResim)
                                {
                                    ProductImage pi = new ProductImage();
                                    pi.ProductID = item.ProductID;
                                    bool   yuklemeSonucu;
                                    string path = FxFonksiyon.ImageUpload(img, "products", out yuklemeSonucu);
                                    if (yuklemeSonucu)
                                    {
                                        pi.ImagePath = path;
                                    }
                                    db.ProductImages.Add(pi);
                                    db.SaveChanges();
                                }
                            }
                        }

                        TempData["Message"] = FxFonksiyon.GetInformation(MessageFormat.OK);
                        ts.Complete();
                        return(RedirectToAction("Index", "Product"));
                    }

                    else
                    {
                        TempData["Message"] = FxFonksiyon.GetInformation(MessageFormat.Val);
                    }
                }
            }
            catch
            {
                TempData["Message"] = FxFonksiyon.GetInformation(MessageFormat.Err);
            }
            return(View());
        }