예제 #1
0
        protected void ButtonAddCategory_Click(object sender, EventArgs e)
        {
            if (!CanAddCategory())
            {
                ErrorSuccessNotifier.AddWarningMessage("Only admins can add categories");
                return;
            }

            var context = new ApplicationDbContext();

            string newCatName = this.TextBoxNewCategory.Text;

            if (string.IsNullOrWhiteSpace(newCatName))
            {
                ErrorSuccessNotifier.AddErrorMessage("Please enter category name");
                return;
            }
            else if (context.Categories.FirstOrDefault(c => c.Name == newCatName) != null)
            {
                ErrorSuccessNotifier.AddErrorMessage("Category with this name already exist");
                return;
            }
            else
            {
                Models.Category cat = new Models.Category() { Name = newCatName };
                context.Categories.Add(cat);
                context.SaveChanges();
                this.TextBoxNewCategory.Text = "";
                this.DataBind();
                ErrorSuccessNotifier.AddSuccessMessage("Category added");
            }
        }