public static void Update(News news, NewsFormVM viewModel) { using (ApplicationDbContext _context = new ApplicationDbContext()) { news.CategoryId = viewModel.CategoryId; news.Content = viewModel.Content; news.Title = viewModel.Title; _context.SaveChanges(); } }
public ActionResult Update(NewsFormVM viewModel) { try { var news = _context.News .Single(n => n.Id == viewModel.Id); NewsManager.Update(news, viewModel); return(RedirectToAction("Index", "Home")); }catch (Exception ex) { throw ex; } }
public ActionResult AddNews() { try { NewsFormVM viewModel = new NewsFormVM { Categories = _context.Categories.ToList() }; return(View("NewsForm", viewModel)); } catch (Exception ex) { throw ex; } }
public static void Add(NewsFormVM viewModel, string userId) { using (ApplicationDbContext _context = new ApplicationDbContext()) { var news = new News { AuthorId = userId, Date = DateTime.Now, Title = viewModel.Title, Content = viewModel.Content, CategoryId = viewModel.CategoryId, imageUrl = viewModel.imageUrl }; _context.News.Add(news); _context.SaveChanges(); } }
public ActionResult Edit(int id) { try { var news = _context.News.Single(n => n.Id == id); var viewModel = new NewsFormVM { Id = news.Id, Title = news.Title, CategoryId = news.CategoryId, Categories = _context.Categories.ToList(), Content = news.Content }; return(View("NewsForm", viewModel)); } catch (Exception ex) { throw ex; } }
public ActionResult AddNews(NewsFormVM viewModel) { try { var userId = User.Identity.GetUserId(); if (!ModelState.IsValid) { var vm = new NewsFormVM { Title = viewModel.Title, CategoryId = viewModel.CategoryId, Content = viewModel.Content, Categories = _context.Categories.ToList() }; return(View("NewsForm", vm)); } switch (viewModel.CategoryId) { case 1: viewModel.imageUrl = "news.png"; break; case 2: viewModel.imageUrl = "biznis.jpg"; break; case 3: viewModel.imageUrl = "sport.jpg"; break; case 4: viewModel.imageUrl = "magazin.jpg"; break; case 5: viewModel.imageUrl = "lifestyle.jpg"; break; case 6: viewModel.imageUrl = "scitech.png"; break; case 7: viewModel.imageUrl = "auto.jpg"; break; case 8: viewModel.imageUrl = "crnahronika.jpg"; break; default: viewModel.imageUrl = "noimage.jpg"; break; } NewsManager.Add(viewModel, userId); return(RedirectToAction("Index", "Home")); } catch (Exception ex) { throw ex; } }