public IActionResult AddUpdateUser(User U, ICollection <IFormFile> Image)
        {
            U.DateCreated = DateTime.Now;
            string wwwroothpath    = env.WebRootPath;
            string ImageFolderPath = wwwroothpath + "/ProjectImages/";

            foreach (var file in Image)
            {
                string Name       = file.Name;
                string FileName   = file.FileName;
                long   FileLenght = file.Length;

                string FileNameWithoutExtenstion = Path.GetFileNameWithoutExtension(FileName);
                Random r = new Random();

                FileNameWithoutExtenstion = DateTime.Now.ToString("ddmmyyyyhhmm") + r.Next(1, 1000).ToString();
                string Extenstion = Path.GetExtension(FileName);

                FileStream fs = new FileStream(ImageFolderPath + FileNameWithoutExtenstion + Extenstion, FileMode.CreateNew);
                file.CopyTo(fs);
                fs.Close();
                fs.Dispose();

                U.Image = ImageFolderPath + FileNameWithoutExtenstion + Extenstion;
            }

            OurDbContext.User.Add(U);
            OurDbContext.SaveChanges();
            return(RedirectToAction(nameof(UserLoginController.AddUpdateUser)));
        }
Exemple #2
0
 public IActionResult AddNewNews(News NewsObj)
 {
     OurDbcontext.News.Add(NewsObj);
     OurDbcontext.SaveChanges();
     return(View());
 }
 public IActionResult AddUpdateNews(ModelNews MN)
 {
     if (!ModelState.IsValid)
     {
         TempData["Action"] = Constants.FAILED;
         return(View(MN));
     }
     try
     {
         if (MN.NewsId > 0)
         {
             MN.DateUpdated = DateTime.Now;
             OurdbContext.News.Update(CopyMNToN(MN));
             OurdbContext.SaveChanges();
         }
         else
         {
             OurdbContext.News.Add(CopyMNToN(MN));
             OurdbContext.SaveChanges();
         }
     }
     catch (Exception)
     {
         TempData["action"] = Constants.FAILED;
     }
     return(RedirectToAction(nameof(AdministratorController.NewsList)));
 }