public ActionResult Create(MyNews news) { try { if (ModelState.IsValid) { if (news == null) { return RedirectToAction("Index"); } News allNews = new News() { NewsID = news.NewsID, NewsTitle = news.NewsTitle, CategoryID = news.CategoryID, Description = news.Description, NoOfViews = 0, IsActive = news.ActiveCheck, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now }; contxt.News.Add(allNews); contxt.SaveChanges(); return RedirectToAction("Index"); } else return RedirectToAction("Index"); } catch (Exception) { throw; } }
public ActionResult Edit(MyNews news) { try { if (ModelState.IsValid) { if (news == null) { return RedirectToAction("Index"); } News oldNewsToModify = (from oldnews in contxt.News where oldnews.NewsID == news.NewsID select oldnews).SingleOrDefault(); oldNewsToModify.NewsTitle = news.NewsTitle; oldNewsToModify.Description = news.Description; oldNewsToModify.CategoryID = news.CategoryID; oldNewsToModify.IsActive = news.ActiveCheck; oldNewsToModify.UpdatedDate = DateTime.Now; contxt.SaveChanges(); return RedirectToAction("Index"); } else return RedirectToAction("Index"); } catch (Exception) { throw; } }
public ActionResult Edit(int newsID) { try { if (newsID == 0) { return RedirectToAction("Index"); } News editNews = (from news in contxt.News where news.NewsID == newsID select news).SingleOrDefault(); MyNews myNews = new MyNews() { NewsTitle = editNews.NewsTitle, Description = editNews.Description, CategoryID = editNews.CategoryID, ActiveCheck = editNews.IsActive == null ? false : editNews.IsActive == false ? false : true, }; return View(myNews); } catch (Exception) { throw; } }