Пример #1
0
        public IActionResult Create(TopicModel model)
        {
            // if valid -> save and send to another page
            if (ModelState.IsValid)
            {
                try
                {
                    // if id is not set -> creating a new topic
                    if (model.Id == 0)
                    {
                        // manager call
                        manager.CreateNew(model.Title);
                    }
                    // ID is defined -> editing topic
                    else
                    {
                        manager.Update(model.Id, model.Title);
                    }

                    return(RedirectToAction(nameof(Index)));
                }
                catch (LogicException ex)
                {
                    ModelState.AddModelError("validation", ex.Message);
                }
                catch (Exception ex)
                {
                    // some other unexpected error
                    ModelState.AddModelError("validation", ex.Message);
                }
            }

            // if not valid -> return back to the same view
            return(View(model));
        }
Пример #2
0
        public IActionResult Create(TopicModel model)
        {
            // if valid -> save and send to another page
            if (ModelState.IsValid)
            {
                try
                {
                    manager.CreateNew(model.Title);

                    return(RedirectToAction(nameof(Create)));
                }
                catch (LogicException ex)
                {
                    ModelState.AddModelError("validation", ex.Message);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("validation", ex.Message);
                }
            }

            // if not valid -> return back to the same view
            return(View(model));
        }