Пример #1
0
 public ActionResult CreateCategory(Category newCat)
 {
     if (ModelState.IsValid)
     {
         if (_repo.UpdateCategory(newCat))
         {
             TempData["message"] = string.Format("{0} успешно сохранен", newCat.Name);
             return RedirectToAction("Categories");
         }
     }
     return View("EditCategory");
 }
        public bool UpdateCategory(Category cat)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    if (cat.Id == 0)
                    {
                        var newCat = cat;
                        context.Categories.Add(newCat);

                    }
                    else
                    {
                        var catFromDb = context.Categories
                            .Include(p => p.Posts)
                            .Single(p => p.Id == cat.Id);

                        context.Entry(catFromDb).CurrentValues.SetValues(cat);

                    }
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {

                    throw;
                    return false;
                }
            }
        }
Пример #3
0
 public ViewResult CreateCategory()
 {
     var newCat = new Category();
     return View("EditCategory", newCat);
 }
        public bool ExcludePostFromCategory(Category chosenCat, int postId)
        {
            using (var context = new EFDbContext())
            {
                try
                {
                    var postToDelete = context.Posts.First(x => x.Id == postId);

                    chosenCat.Posts
                        .Remove(postToDelete);

                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }