Example #1
0
        public async Task <IActionResult> Upsert()
        {
            if (ModelState.IsValid)
            {
                //Save file to vernumBlog application if the file is given
                if (Request.Form.Files.Count() != 0)
                {
                    var file = Request.Form.Files.First();

                    //savePath has to be changed to vernumBlog solution folder in the computer
                    string savePath = Path.Combine("C:", "Users", "cemoz", "OneDrive",
                                                   "Masaüstü", "Programlama", "ASP.NET", "VernumBlog", "wwwroot", "img");

                    //In order to avoid overriding files with same names. We set file names to creation date
                    var fileName = $"{DateTime.Now:MMddHHmmss}.{file.FileName.Split(".").Last()}";
                    var fileUrl  = Path.Combine(savePath, fileName);
                    using (var fileStream = new FileStream(fileUrl, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    Category.imagePath = fileName;
                }

                if (Category.Posts == null)
                {
                    Category.Posts = await _db.Posts.Where(p => p.categoryId == Category.Id).ToListAsync();
                }

                //If its a new category create new category
                if (Category.Id == 0)
                {
                    await _db.Categories.AddAsync(Category);
                }
                //Otherwise update it
                else
                {
                    _db.Categories.Update(Category);
                }

                await _db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(Category));
        }
Example #2
0
        public async Task <IActionResult> Upsert()
        {
            if (ModelState.IsValid)
            {
                //Save file to vernumBlog application, if the file is given
                if (Request.Form.Files.Count() != 0)
                {
                    var file = Request.Form.Files.First();

                    //savePath has to be changed to vernumBlog solution folder in the computer
                    string savePath = Path.Combine("C:", "Users", "cemoz", "OneDrive",
                                                   "Masaüstü", "Programlama", "ASP.NET", "VernumBlog", "wwwroot", "img");
                    var fileName = $"{DateTime.Now:MMddHHmmss}.{file.FileName.Split(".").Last()}";
                    var fileUrl  = Path.Combine(savePath, fileName);
                    using (var fileStream = new FileStream(fileUrl, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    Post.imagePath = fileName;
                }

                //If its a new post create new post
                if (Post.Id == 0)
                {
                    _db.Posts.Add(Post);
                }
                //Otherwise update it
                else
                {
                    _db.Posts.Update(Post);
                }
                await _db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(Post));
        }