Ejemplo n.º 1
0
 public async Task <ActionResult> Create(BazinamSite2.Areas.Admin.ViewModel.News _new)
 {
     try
     {
         // TODO: Add insert logic here
         if (ModelState.IsValid)
         {
             BazinamSite2.Models.News news = new BazinamSite2.Models.News()
             {
                 Title       = _new.Title,
                 Content     = _new.Content,
                 ReleaseDate = DateTime.Parse(_new.ReleaseDate)
             };
             if (Session["NewsFile"] != null)
             {
                 var files = (List <string>)Session["NewsFile"];
                 foreach (var fileName in files)
                 {
                     byte[] result;
                     using (FileStream SourceStream = System.IO.File.Open(Server.MapPath("~/Areas/Admin/TempPic/") + fileName, FileMode.Open))
                     {
                         result = new byte[SourceStream.Length];
                         await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
                     }
                     BazinamSite2.Models.Picture pic = new BazinamSite2.Models.Picture()
                     {
                         PicName        = fileName,
                         PicSourceBytes = result,
                         PicUrl         = "",
                         IsRefrence     = false
                     };
                     news.PicturescCollection.Add(pic);
                 }
             }
             using (SystemDbContext context = new SystemDbContext())
             {
                 context.News.Add(news);
                 await context.SaveChangesAsync();
             }
         }
         return(RedirectToAction("/Index"));
     }
     catch
     {
         return(View());
     }
 }
Ejemplo n.º 2
0
 public async Task <ActionResult> Edit(int id, BazinamSite2.Areas.Admin.ViewModel.News _new)
 {
     try
     {
         // TODO: Add update logic here
         var editedNews = new BazinamSite2.Models.News()
         {
             NewsID      = id,
             Title       = _new.Title,
             Content     = _new.Content,
             ReleaseDate = DateTime.Parse(_new.ReleaseDate)
         };
         using (SystemDbContext context = new SystemDbContext())
         {
             context.Entry(editedNews).State = System.Data.Entity.EntityState.Modified;
             await context.SaveChangesAsync();
         }
         return(RedirectToAction("/Index"));
     }
     catch
     {
         return(View());
     }
 }