Beispiel #1
0
        public async Task <IActionResult> Edit(Page page)
        {
            //check if model is valid
            if (ModelState.IsValid)
            {
                //check if edited page is home, return slug='home' else set the new slug according
                page.Slug = page.Id == 1 ? "home": page.Slug = page.Title.ToLower().Replace(" ", "-");

                //search the slug except the current and see if return something
                var slug = await context.Pages.Where(x => x.Id != page.Id).FirstOrDefaultAsync(x => x.Slug == page.Slug);

                if (slug != null)
                {
                    ModelState.AddModelError("", "The page allready exists");
                    return(View(page));
                }

                //add the page to db
                context.Update(page);
                await context.SaveChangesAsync();

                TempData["Success"] = "The page has been edited!";


                return(RedirectToAction("Edit", new { id = page.Id }));
            }

            return(View(page));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit(int id, Category category)
        {
            //check if model is valid
            if (ModelState.IsValid)
            {
                category.Slug = category.Name.ToLower().Replace(" ", "-");

                //search the category except the current and see if return something
                var slug = await context.Categories.Where(x => x.Id != category.Id).FirstOrDefaultAsync(x => x.Slug == category.Slug);

                if (slug != null)
                {
                    ModelState.AddModelError("", "The category allready exists");
                    return(View(category));
                }

                //add the page to db
                context.Update(category);
                await context.SaveChangesAsync();

                TempData["Success"] = "The category has been edited!";


                return(RedirectToAction("Edit", new { id }));
            }

            return(View(category));
        }
        public async Task <IActionResult> Edit(int id, Product product)
        {
            ViewBag.CategoryId = new SelectList(context.Categories.OrderBy(x => x.Sorting), "Id", "Name", product.CategoryId);

            //check if model is valid
            if (ModelState.IsValid)
            {
                //set the page prop
                product.Slug = product.Name.ToLower().Replace(" ", "-");

                //get the slug which is equal to current
                var slug = await context.Products.Where(x => x.Id != id).FirstOrDefaultAsync(x => x.Slug == product.Slug);

                if (slug != null)
                {
                    ModelState.AddModelError("", "The product allready exists");
                    return(View(product));
                }

                //edit the image file
                if (product.ImageUpload != null)
                {
                    //set the directory using webHostEnvironment
                    string uploadsDir = Path.Combine(webHostEnvironment.WebRootPath, "media/products");

                    //remove the old image if exists one
                    if (!string.Equals(product.Image, "noimage.png"))
                    {
                        string oldImagePath = Path.Combine(uploadsDir, product.Image);
                        if (System.IO.File.Exists(oldImagePath))
                        {
                            System.IO.File.Delete(oldImagePath);
                        }
                    }

                    //set the name of the image to be unique
                    string imageName = Guid.NewGuid().ToString() + "_" + product.ImageUpload.FileName;
                    //set the full image path
                    string filePath = Path.Combine(uploadsDir, imageName);

                    //upload using FileStream class
                    FileStream fs = new FileStream(filePath, FileMode.Create);
                    await product.ImageUpload.CopyToAsync(fs);

                    fs.Close();
                    //set the property name
                    product.Image = imageName;
                }


                //add the page to db
                context.Update(product);
                await context.SaveChangesAsync();

                TempData["Success"] = "The product has been edited!";

                return(RedirectToAction("Index"));
            }

            return(View(product));
        }