コード例 #1
0
        public ActionResult Edit(int id = 0)
        {
            List <Category> allCategories = uow.CategoryRepository.GetAll();

            ViewBag.AllCategories = new SelectList(allCategories, "ID", "Name");

            StoryViewModel model = new StoryViewModel();

            Story dbStory = uow.StoryRepository.GetByID(id);

            if (dbStory != null)
            {
                model = new StoryViewModel(dbStory);
            }
            return(View(model));
        }
コード例 #2
0
        public ActionResult Edit(StoryViewModel viewModel)
        {
            if (viewModel == null)
            {
                TempData["Message"] = "No View Model";
                return(RedirectToAction("Index"));
            }

            if (Request.Files.Count == 0 && !viewModel.HasImage)
            {
                TempData["Message"] = "Story must have a valid image";
                return(RedirectToAction("Edit", viewModel.ID));
            }

            Story dbStory = uow.StoryRepository.GetByID(viewModel.ID);

            if (dbStory == null)
            {
                dbStory = new Story();
            }

            dbStory.Category = viewModel.Category;
            dbStory.Name     = viewModel.Name;
            dbStory.Slug     = viewModel.Slug;

            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];
                if (file.ContentLength != 0)
                {
                    string uniqueFileName = string.Format("{0}_{1}", DateTime.Now.Millisecond, file.FileName);
                    string savedFileName  = Path.Combine(Server.MapPath(Constants.ImagesDirectory), Path.GetFileName(uniqueFileName));
                    file.SaveAs(savedFileName.Replace(' ', '_'));
                    dbStory.ImgLink = uniqueFileName.Replace(' ', '_');
                }
            }

            uow.StoryRepository.Save(dbStory);

            uow.Save();

            TempData["Message"] = "The story was saved successfully";
            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public ActionResult Index(SearchViewModel searchModel)
        {
            if (searchModel.Story == null)
            {
                searchModel.Story = "";
            }

            List <Category> categories = uow.CategoryRepository.GetAll();

            List <CategoriesViewModel> categoriesViewModel = new CategoryViewModel(categories).Categories.ToList();

            searchModel.Categories = categoriesViewModel;

            List <Story> stories = uow.StoryRepository.GetStoryByCategory(uow.CategoryRepository.GetCategoryTree(searchModel.Category))
                                   .Where(s => s.Name.Contains(searchModel.Story)).ToList();

            StoryViewModel model = new StoryViewModel(stories);

            searchModel.Stories = model;

            return(View(searchModel));
        }
コード例 #4
0
        public ActionResult EditStories()
        {
            StoryViewModel stories = new StoryViewModel(uow.StoryRepository.GetAll());

            return(View(stories));
        }