public ActionResult Update(NewsFormViewModel viewmodel) { if (!ModelState.IsValid) { viewmodel.Authors = db.Authors.ToList(); return(View("NewsForm", viewmodel)); } var userId = User.Identity.GetUserId(); var news = db.News .Single(n => n.Id == viewmodel.Id && n.UserId == userId); news.Title = viewmodel.Title; news.PublicationDate = viewmodel.PublicationDate; news.NewsDescription = viewmodel.NewsDescription; news.AuthorId = viewmodel.Author; var oldpath = Path.Combine(Server.MapPath("~/Images"), news.Image); if (viewmodel.ImageUpload != null) { System.IO.File.Delete(oldpath); var path = Path.Combine(Server.MapPath("~/Images"), viewmodel.ImageUpload.FileName); viewmodel.ImageUpload.SaveAs(path); news.Image = viewmodel.ImageUpload.FileName; } db.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public ActionResult Save(NewsArticle newsArticle) { if (!ModelState.IsValid) { NewsFormViewModel newsFormView = new NewsFormViewModel { NewsArticle = newsArticle, NewsCategories = _repository.GetNewsCategories() }; return(View(ViewName.NewsAdmin_NewsForm, newsFormView)); } if (newsArticle.Id == 0) { newsArticle.Author = User.Identity.Name; newsArticle.PublishDate = DateTime.Now; _repository.AddNew(newsArticle); } else { _repository.Update(newsArticle); } return(RedirectToAction(ViewName.NewsAdmin_Index, ControllerName.NewsAdmin)); }
public ViewResult NewNews() { NewsFormViewModel newsFormView = new NewsFormViewModel { NewsArticle = new NewsArticle(), NewsCategories = _repository.GetNewsCategories() }; return(View(ViewName.NewsAdmin_NewsForm, newsFormView)); }
public ActionResult Create() { var viewmodel = new NewsFormViewModel { Authors = db.Authors.ToList(), Heading = "Add News" }; return(View("NewsForm", viewmodel)); }
public ActionResult EditNews(int id) { NewsArticle article = _repository.GetById(id); if (article == null) { return(HttpNotFound()); } else { NewsFormViewModel newsFormView = new NewsFormViewModel { NewsArticle = article, NewsCategories = _repository.GetNewsCategories() }; return(View(ViewName.NewsAdmin_NewsForm, newsFormView)); } }
public ActionResult Edit(int id) { var userId = User.Identity.GetUserId(); var news = db.News.Single(n => n.Id == id && n.UserId == userId); var viewmodel = new NewsFormViewModel() { Id = id, Authors = db.Authors.ToList(), Title = news.Title, PublicationDate = news.PublicationDate, NewsDescription = news.NewsDescription, Author = news.AuthorId, Image = news.Image, Heading = "Edit News" }; return(View("NewsForm", viewmodel)); }
public void NewNews_OnRequest_HaveEmptyArticleAndAllCategories() { FakeNewsRepository fakeNewsRepository = new FakeNewsRepository(); NewsAdminController controller = CreateNewsAdminController(fakeNewsRepository); ViewResult result = controller.NewNews(); NewsFormViewModel viewModel = (NewsFormViewModel)result.Model; List <int> expectedCategories = new List <int> { 0, 1, 2, 3, 4 }; Assert.IsTrue(TestHelper.NewsArticlesAreSame(viewModel.NewsArticle, new NewsArticle())); Assert.IsTrue(TestHelper.AreDataAsExpected <NewsCategory>(viewModel.NewsCategories.ToList(), fakeNewsRepository.Categories, expectedCategories, TestHelper.NewsCategoriesAreSame)); }
public ActionResult AddNews(NewsFormViewModel model, HttpPostedFileBase upload) { var news = new News { Headline = model.Headline, Text = model.Text, Author = model.Author }; if (upload != null && upload.ContentLength > 0) { using (var reader = new System.IO.BinaryReader(upload.InputStream)) { news.Picture = reader.ReadBytes(upload.ContentLength); } } db.AddNews(news); return(RedirectToAction("Index")); }
public ActionResult Create(NewsFormViewModel viewmodel) { if (!ModelState.IsValid) { viewmodel.Authors = db.Authors.ToList(); return(View("NewsForm", viewmodel)); } if (viewmodel.ImageUpload != null) { var path = Path.Combine(Server.MapPath("~/Images"), viewmodel.ImageUpload.FileName); viewmodel.ImageUpload.SaveAs(path); viewmodel.Image = viewmodel.ImageUpload.FileName; } //if (viewmodel.ImageUpload != null) //{ // string filename = Path.GetFileNameWithoutExtension(viewmodel.ImageUpload.FileName); // string extension = Path.GetExtension(viewmodel.ImageUpload.FileName); // filename = filename + DateTime.Now.ToString("yymmssfff") + extension; // viewmodel.Image = "~/Images/" + filename; // filename = Path.Combine(Server.MapPath("~/Images/"), filename); // viewmodel.ImageUpload.SaveAs(filename); //} var userId = User.Identity.GetUserId(); var news = new New() { CreationDate = DateTime.Now, Title = viewmodel.Title, PublicationDate = viewmodel.PublicationDate, Image = viewmodel.Image, NewsDescription = viewmodel.NewsDescription, AuthorId = viewmodel.Author, UserId = userId }; db.News.Add(news); db.SaveChanges(); return(RedirectToAction("Index", "Home")); }