Inheritance: System.Data.Objects.DataClasses.EntityObject
コード例 #1
0
        public void Can_Not_Save_Category_That_Already_Exists()
        {
            var category = new Category { Name = "Hockey" };

            var result = controller.Edit(category);

            mock.Verify(m => m.SaveCategory(It.IsAny<Category>()), Times.Never());
            result.AssertViewRendered().ForView(string.Empty);
        }
コード例 #2
0
 public ActionResult Edit(Category category)
 {
     if (ModelStateAndCategoryNameIsValid(category))
     {
         if (CategoryAlreadyExists(category))
         {
             this.Error("A category with that name already exists.");
             //ModelState.AddModelError("", "A category with that name already exists.");
             return View(category);
         }
         else
         {
             repository.SaveCategory(category);
             this.Success("The category has been saved");
             return RedirectToAction("Index");
         }
     }
     return View(category);
 }
コード例 #3
0
 /// <summary>
 /// Create a new Category object.
 /// </summary>
 /// <param name="categoryId">Initial value of the CategoryId property.</param>
 /// <param name="isActive">Initial value of the IsActive property.</param>
 public static Category CreateCategory(global::System.Int32 categoryId, global::System.Boolean isActive)
 {
     Category category = new Category();
     category.CategoryId = categoryId;
     category.IsActive = isActive;
     return category;
 }
コード例 #4
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Categories EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCategories(Category category)
 {
     base.AddObject("Categories", category);
 }
コード例 #5
0
 private bool ModelStateAndCategoryNameIsValid(Category category)
 {
     return ModelState.IsValid && !string.IsNullOrEmpty(category.Name);
 }
コード例 #6
0
        private bool CategoryAlreadyExists(Category category)
        {
            Category existingCategory = repository.Categories
                    .FirstOrDefault(c => c.Name == category.Name);

            return existingCategory != null && category.CategoryId == 0;
        }
コード例 #7
0
 public void SaveCategory(Category category)
 {
     if (category.CategoryId == 0)
     {
         context.AddToCategories(category);
     }
     else
     {
         try
         {
             context.UpdateDetachedEntity<Category>(category, x => x.CategoryId);
         }
         catch (Exception)
         {
             context.ApplyCurrentValues<Category>(category.EntityKey.EntitySetName, category);
         }
     }
     context.SaveChanges();
 }
コード例 #8
0
        public void Can_Not_Save_Category_With_Empty_Name()
        {
            var category = new Category { Name = null };

            var result = controller.Edit(category);

            mock.Verify(m => m.SaveCategory(It.IsAny<Category>()), Times.Never());
            result.AssertViewRendered().ForView(string.Empty);
        }