public async Task <IActionResult> Create([Bind("FirstSubCatId,CatId_FK,FirstSubCatTitle,Picture")] FirstSubCategory firstSubCategory, IFormFile CatPic)
        {
            if (ModelState.IsValid)
            {
                string fileName   = "";
                string uploadPath = "";
                if (CatPic != null && CatPic.Length > 0)
                {
                    fileName   = Guid.NewGuid().ToString().Replace("-", "") + CatPic.FileName.ToLower();
                    uploadPath = Path.Combine(environment.WebRootPath, "Files\\Images\\");
                    using (var fileStream = new FileStream(Path.Combine(uploadPath, fileName), FileMode.Create))
                    {
                        await CatPic.CopyToAsync(fileStream);
                    }
                    database.Tbl_FirstSubCategory.Add(new FirstSubCategory()
                    {
                        CatId_FK         = firstSubCategory.CatId_FK,
                        FirstSubCatTitle = firstSubCategory.FirstSubCatTitle,
                        Picture          = fileName,
                    });
                }

                //database.Add(firstSubCategory);
                await database.SaveChangesAsync();

                //return RedirectToAction(nameof(Index));
            }
            //return View(firstSubCategory);
            return(RedirectToAction(nameof(Index)));
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, [Bind("FirstSubCatId,CatId_FK,FirstSubCatTitle,Picture")] FirstSubCategory firstSubCategory, IFormFile CatPic)
        {
            var q = database.Tbl_FirstSubCategory.Where(c => c.FirstSubCatId == id).SingleOrDefault();

            if (id != q.FirstSubCatId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    string oldFilename = q.Picture;
                    string fileName    = "";
                    string uploadPath  = "";
                    database.Update(q);

                    if (CatPic != null && CatPic.Length > 0)
                    {
                        string RemovePic = Path.Combine(environment.WebRootPath, "Files\\Images\\" + oldFilename);
                        if (System.IO.File.Exists(RemovePic))
                        {
                            System.IO.File.Delete(RemovePic);
                        }
                        fileName   = Guid.NewGuid().ToString().Replace("-", "") + CatPic.FileName.ToLower();
                        uploadPath = Path.Combine(environment.WebRootPath, "Files\\Images\\");
                        using (var fileStream = new FileStream(Path.Combine(uploadPath, fileName), FileMode.Create))
                        {
                            await CatPic.CopyToAsync(fileStream);
                        }
                    }
                    q.CatId_FK         = firstSubCategory.CatId_FK;
                    q.FirstSubCatTitle = firstSubCategory.FirstSubCatTitle;
                    q.Picture          = fileName == "" ? oldFilename : fileName;
                    database.Update(q);
                    await database.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FirstSubCategoryExists(q.FirstSubCatId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                //return RedirectToAction(nameof(Index));
            }
            return(RedirectToAction(nameof(Index)));
            //return View(firstSubCategory);
        }