protected void CreateCategoryButton_Command(object sender, CommandEventArgs e)
        {
            var context = new LibrarySystemEntities();

            string categoryName = this.CreateCategoryField.Text;

            if (string.IsNullOrWhiteSpace(categoryName))
            {
                ErrorSuccessNotifier.AddErrorMessage("Category name cannot be empty");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            if (categoryName.Length < 3 || categoryName.Length > 100)
            {
                ErrorSuccessNotifier.AddErrorMessage("Category length should be  between 3 and 100 symbols");
                this.Response.Redirect("~/Admin/EditCategories.aspx");
            }

            Category newCategory = new Category();
            newCategory.CategoryName = categoryName;

            context.Categories.Add(newCategory);
            context.SaveChanges();

            ErrorSuccessNotifier.AddSuccessMessage("Category created!");
            this.Response.Redirect("~/Admin/EditCategories.aspx");
        }
Пример #2
0
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(category);
        }
        public void ListViewCategories_InsertItem()
        {
            var item = new Category();
            TryUpdateModel(item);

            if (ModelState.IsValid)
            {
                dbContext.Categories.Add(item);
                dbContext.SaveChanges();
            }
        }
 public ActionResult CreateCommand([DataSourceRequest] DataSourceRequest request, CategoryVewModel model)
 {
     var entity = new Category();
     if (ModelState.IsValid)
     {
         entity.Name = model.Name;
         Data.Categories.Add(entity);
         this.Data.SaveChanges();
         model.Id = entity.Id;
     }
     return Json(new[] { model }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
 }
Пример #5
0
        public void CreateCategoryFormView_InsertItem()
        {
            LibraryDbContext db = new LibraryDbContext();
            var item            = new LibrarySystem.Models.Category();

            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                db.Categories.Add(item);
                db.SaveChanges();
            }

            this.CreateCategoryPanel.Visible = false;
            this.Response.Redirect(this.Request.RawUrl);
        }
        public ActionResult CategoriesDelete([DataSourceRequest]DataSourceRequest request, CategoryViewModel category)
        {
            if (ModelState.IsValid)
            {
                var toDelete = new Category()
                {
                    ID = category.Id,
                    Name = category.Name
                };

                this.Data.Categories.Attach(toDelete);
                this.Data.Categories.Remove(toDelete);
                this.Data.SaveChanges();
            }
            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
        public ActionResult CategoriesUpdate([DataSourceRequest]DataSourceRequest request, CategoryViewModel category)
        {
            if (ModelState.IsValid)
            {
                var newCategory = new Category()
                {
                    ID = category.Id,
                    Name = category.Name
                };
                this.Data.Categories.Attach(newCategory);
                this.Data.Entry(newCategory).State = EntityState.Modified;
                this.Data.SaveChanges();

            }
            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
        public ActionResult CategoriesCreate([DataSourceRequest]DataSourceRequest request, CategoryViewModel category)
        {
            if (ModelState.IsValid)
            {
                Category newCategory = new Category()
                {
                    Name = category.Name
                };

                newCategory.ID= this.Data.Categories.Add(newCategory).ID;
                this.Data.SaveChanges();
                category.Id = newCategory.ID;
            }

            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
        public ActionResult CategoriesCreate([DataSourceRequest]DataSourceRequest request, CategoryViewModel Category)
        {
            if (ModelState.IsValid)
            {
                var entity = new Category
                {
                    Name = Category.Name,
                };

                db.Categories.Add(entity);
                db.SaveChanges();
                Category.Name = entity.Name;
            }

            return Json(new[] { Category }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
        {
            if (category != null && ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(category.Name))
                {
                    ModelState.AddModelError("CategoryName", "The category name is required.");
                }

                var newCategory = new Category() { Name = category.Name };
                db.Categories.Add(newCategory);
                db.SaveChanges();
            }

            return Json(new[] { category }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
Пример #11
0
 private static Category CreateOrGetCategory(BookViewModel book, LibraryDbContext db, Book newBook, Category category)
 {
     if (category == null)
     {
         category = db.Categories.Add(new Category()
         {
             Name = book.CategoryName
         });
         category.Books = new HashSet<Book>();
         category.Books.Add(newBook);
     }
     else
     {
         newBook.Category = category;
     }
     return category;
 }
Пример #12
0
        public ActionResult CategoriesCreate([DataSourceRequest]DataSourceRequest request, CategoryViewModel category)
        {
            if (ModelState.IsValid)
            {
                var db = new LibraryDbContext();

                Category newCategory = new Category()
                {
                    Name = category.Name
                };

                newCategory.Id = db.Categories.Add(newCategory).Id;
                db.SaveChanges();
                category.Id = newCategory.Id;
            }
            
            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
        protected void LinkButtonCreate_OnClick(object sender, EventArgs e)
        {
            this.Page.Validate("ValidateAddCategoryGroup");
            if (Page.IsValid)
            {
                string categoryName = this.TextBoxAddCategory.Text;
                this.TextBoxAddCategory.Text = string.Empty;
                var context = new ApplicationDbContext();

                var category = new Category()
                {
                    Name = categoryName
                };
                context.Categories.Add(category);
                context.SaveChanges();
                this.PanelCreateCategory.Visible = false;
                Response.Redirect("EditCategories.aspx");
            }
        }
Пример #14
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtCategoryName.Text))
            {
                var categoryStatus = ActionType.Modified;

                var db = new ApplicationDbContext();
                var category = db.Categories.Find(btnSave.CommandArgument.ToInt());

                if (category == null && btnSave.CommandName != "Delete")
                {
                    category = new Category();
                    db.Categories.Add(category);
                    categoryStatus = ActionType.Created;
                }

                if (btnSave.CommandName == "Delete")
                {
                    db.Books.RemoveRange(category.Books);
                    db.Categories.Remove(category);
                    categoryStatus = ActionType.Deleted;
                }
                else
                {
                    category.Name = txtCategoryName.Text;
                }

                db.SaveChanges();
                grdCategories.DataBind();
                if (grdCategories.PageIndex == grdCategories.PageCount)
                {
                    grdCategories.PageIndex = 0;
                }

                ErrorSuccessNotifier.AddSuccessMessage("Category " + categoryStatus.ToString());
                CreatePanelVisibility(false);
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage("Category Name is required!");
            }
        }
Пример #15
0
        protected void LinkButtonCreate_Click(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                return;
            }

            string categoryName = this.TextBoxNewCategory.Text;
            if (string.IsNullOrWhiteSpace(categoryName))
            {
                this.LabelErrorMessage.Text = "Category name is required.";
                this.PanelNewCategory.Visible = true;
                return;
            }

            var category = new Category
            {
                Name = categoryName
            };

            var context = new ApplicationDbContext();

            context.Categories.Add(category);
            context.SaveChanges();

            this.PanelNewCategory.Visible = false;
            this.TextBoxNewCategory.Text = string.Empty;
            this.LinkButtonNewCategory.Visible = true;

            Response.Redirect(Request.RawUrl);
        }
 protected void LinkButtonSaveCategory_Click(object sender, EventArgs e)
 {
     try
     {
         LibrarySystemEntities context = new LibrarySystemEntities();
         Category cat = new Category();
         cat.Title = this.TextBoxCategoryName.Text;
         context.Categories.Add(cat);
         context.SaveChanges();
         HideAllPanels();
         this.GridViewCategories.DataBind();
         ErrorSuccessNotifier.AddSuccessMessage("Category created.");
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }
Пример #17
0
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
        {
            if (ModelState.IsValid)
            {
                var categoryToCreate = new Category() 
                {
                    Id = category.Id,
                    Name = category.Name
                };

                db.Categories.Add(categoryToCreate);
                db.SaveChanges();
            }

            return Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult());
        }
        protected void LinkButtonCreateConfirm_Click(object sender, EventArgs e)
        {
            var categoryName = this.TextBoxCreateCategoryName.Text.Trim();
            if (categoryName.Length < 2)
            {
                ErrorSuccessNotifier.AddErrorMessage("Category name must consist of at least 2 symbols!");
                return;
            }

            var context = new LibrarySystemEntities();
            if (context.Categories.Any(c => c.Name == categoryName))
            {
                ErrorSuccessNotifier.AddErrorMessage("Category with this name already exists!");
                return;
            }

            var newCategory = new Category();
            newCategory.Name = categoryName;
            context.Categories.Add(newCategory);

            try
            {
                context.SaveChanges();
                this.GridViewCategories.DataBind();
                this.TextBoxCreateCategoryName.Text = string.Empty;
                ErrorSuccessNotifier.AddSuccessMessage("Category successfully created!");
            }
            catch (Exception exc)
            {
                ErrorSuccessNotifier.AddErrorMessage(exc);
            }
        }
Пример #19
0
 public ActionResult Edit(Category category)
 {
     if (ModelState.IsValid)
     {
         db.Entry(category).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(category);
 }
        protected void CreateBtn_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var context = new LibrarySystemEntities();
                string title = this.CategoryTitleTb.Text;
                Category category = new Category()
                {
                    Name = title
                };

                try
                {
                    context.Categories.Add(category);
                    context.SaveChanges();
                    this.CategoryTitleTb.Text = "";
                    this.InsertForm.Visible = false;
                    this.EditCategoriesList.DataBind();

                    ErrorSuccessNotifier.AddSuccessMessage("Category created");
                }
                catch (Exception ex)
                {

                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    Response.Redirect("../EditCategories.aspx");
                }
            }

        }
Пример #21
0
 public Book()
 {
     this.Category = new Category();
 }