Пример #1
0
        public ActionResult Create(NewsFormViewModel newsViewModel)
        {
            if (ViewData.ModelState.IsValid)
            {
                var selectedCategory = newsCategoryRepository.Get(newsViewModel.SelectedCategoryId);
                var news = new News(newsViewModel.Title) { Body = newsViewModel.Body, NewsCategory = selectedCategory };
                newsRepository.SaveOrUpdate(news);
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = "The news was successfully created.";
                return RedirectToAction("Index");
            }

            prepareViewModel(newsViewModel);
            return View(newsViewModel);
        }
Пример #2
0
 private NewsFormViewModel prepareViewModel(NewsFormViewModel viewModel)
 {
     var categories = newsCategoryRepository
         .GetAll();
     viewModel.NewsCategories = categories;
     return viewModel;
 }
Пример #3
0
        public ActionResult Edit(NewsFormViewModel newsFormViewModel)
        {
            var news = newsRepository.Get(newsFormViewModel.Id);
            var category = newsCategoryRepository.Get(newsFormViewModel.SelectedCategoryId);

            if (ViewData.ModelState.IsValid)
            {
                news.Title = newsFormViewModel.Title;
                news.Body = newsFormViewModel.Body;
                news.NewsCategory = category;
                newsRepository.SaveOrUpdate(news);
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = "The news was successfully updated.";
                return RedirectToAction("Index");
            }

            newsRepository.DbContext.RollbackTransaction();
            prepareViewModel(newsFormViewModel);
            return View(newsFormViewModel);
        }