コード例 #1
0
ファイル: EfBlogRepository.cs プロジェクト: silne30/TPCv3
 public int AddCategory(Category category)
 {
     if (_context.Entry(category).State == EntityState.Detached)
     {
         _context.Categories.Attach(category);
     }
     _context.Categories.Add(category);
     _context.SaveChanges();
     return category.Id;
 }
コード例 #2
0
 public static MvcHtmlString CategoryLink(this HtmlHelper helper, Category category)
 {
     return helper.ActionLink(
         category.Name,
         "Category",
         "Blog",
         new {selectedCategory = category.UrlSlug},
         new
             {
                 title = String.Format("See all posts about {0}.", category.Name),
                 @class = "categoryLink"
             });
 }
コード例 #3
0
ファイル: EfBlogRepository.cs プロジェクト: silne30/TPCv3
        public void EditCategory(Category category)
        {
            DbEntityEntry<Category> entry = _context.Entry(category);
            if (entry.State == EntityState.Detached)
            {
                DbSet set = _context.Set(category.GetType());

                var attachedEntity = (Category) set.Find(category.Id);
                if (attachedEntity != null)
                {
                    DbEntityEntry<Category> attachedEntry = _context.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(category);
                }
            }
            else
            {
                entry.State = EntityState.Modified;
            }
            _context.SaveChanges();
        }
コード例 #4
0
ファイル: AdminController.cs プロジェクト: silne30/TPCv3
        public ContentResult EditCategory(Category category)
        {
            string json;

            if (ModelState.IsValid){
                _blogRepository.EditCategory(category);
                json = JsonConvert.SerializeObject(new
                                                       {
                                                           id = category.Id,
                                                           success = true,
                                                           message = "Changes saved successfully."
                                                       });
            }
            else{
                json = JsonConvert.SerializeObject(new
                                                       {
                                                           id = 0,
                                                           success = false,
                                                           message = "Failed to save the changes."
                                                       });
            }

            return Content(json, "application/json");
        }