Exemplo n.º 1
0
        public ActionResult Create(TopicViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var repository = new TopicRepository(context);
                    var topicQry   = new Topic {
                        Name = model.Name
                    };
                    var nombreExiste = repository.QueryByExample(topicQry).Count > 0;
                    if (!nombreExiste)
                    {
                        var topic = MapperHelper.Map <Topic>(model);
                        repository.Insert(topic);
                        context.SaveChanges();
                    }
                    else
                    {
                        ModelState.AddModelError("Name", "El nombre ya está ocupado por otro tópico.");
                        return(View(model));
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ViewBag.Error = ex.Message;
                return(View());
            }
        }