Exemplo n.º 1
0
        public ActionResult Add(PostViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var post = new Post { Id = viewModel.Id, Title = viewModel.Title, Content = viewModel.Content, CreateDate = DateTime.Now, UserName = viewModel.UserName };

                    _db.Posts.Add(post);
                    _db.SaveChanges();

                    return Content(Boolean.TrueString);
                }

                return Content(ExceptionHelper.ModelStateErrorFormat(ModelState));
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionMessageFormat(ex, true);
                return Content("Sorry! Unable to add this post.");
            }
        }
Exemplo n.º 2
0
        public ActionResult Edit(PostViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var post = new Post { Id = viewModel.Id, Title = viewModel.Title, Content = viewModel.Content, CreateDate = viewModel.CreateDate};

                    _db.Entry(post).State = EntityState.Modified;
                    _db.SaveChanges();

                    return Content(Boolean.TrueString);
                }

                return Content(ExceptionHelper.ModelStateErrorFormat(ModelState));
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionMessageFormat(ex, true);
                return Content("Sorry! Unable to edit this post.");
            }
        }