Example #1
0
        public IActionResult Index([Bind(include: "Name, NavigateBackMonth, NavigateBackYear")] CategoryViewModel cat)
        {
            // if the modelstate is okay and the category doesn't already exist
            // add it to the database
            if (ModelState.IsValid)
            {
                // take the user input, create a category with user's identity
                var category = new Category
                {
                    Name = cat.Name,
                    User = User.Identity.Name.ToString()
                };
                if (!_categories.Exists(category))
                {
                    _categories.Add(category);
                    _categories.Commit();
                    return(RedirectToAction("Index", new
                    {
                        month = cat.NavigateBackMonth,
                        year = cat.NavigateBackYear
                    }));
                }

                return(RedirectToAction("Index", new
                {
                    err = "Category already exists",
                    month = cat.NavigateBackMonth,
                    year = cat.NavigateBackYear
                }));
            }
            // otherwise, redirect back to the form, and send the error message
            return(RedirectToAction("Index", new
            {
                err = "Category names must be between 5 and 50 characters long, and can only contain letters, numbers and spaces",
                month = cat.NavigateBackMonth,
                year = cat.NavigateBackYear
            }));
        }