コード例 #1
0
      public IActionResult CreateNewCategory(CategoriesViewModel model)
      {
          bool isAjaxCall = Request.Headers["x-requested-with"] == "XMLHttpRequest";

          try
          {
              if (ModelState.IsValid)   // Server check in case JS is disabled
              {
                  //if (model.NewCategoryName != "Uncategorized")
                  //{
                  //    _dbHelper.AddCategory(model.NewCategoryName);
                  //    TempData["Success"] = true;
                  //}
                  //else
                  //{
                  //    TempData["ErrorMessage"] = "Rubriku s tímto názvem není možné vytvořit.";
                  //}

                  // AddCategory checks for 'Uncategorized' category
                  _dbHelper.AddCategory(model.NewCategoryName);
                  TempData["Success"] = true;
              }
              else
              {
                  // Categories are not passed back from view, so they need to be filled into model again
                  // model.Categories = _dbHelper.GetAllCategories();
                  throw new Exception();
              }
          }
          catch (Exception ex)
          {
              TempData["ErrorMessage"] = ex.Message + " " + ((ex.InnerException != null) ? ex.InnerException.Message : string.Empty);
          }

          if (isAjaxCall)
          {
              ModelState.Clear();
              return(PartialView("AddNewCategoryPartialView", UpdateCategoryPage()));
          }
          else
          {
              return(RedirectToAction("Index"));
          }
      }