示例#1
0
        public ActionResult Create(EC_News ec_news)
        {
            if (ModelState.IsValid)
            {
                db.EC_News.Add(ec_news);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.Author = new SelectList(db.EC_Users, "Id", "Username", ec_news.Author);
            return(View(ec_news));
        }
示例#2
0
        public ActionResult Create(NewsViewModel _new)
        {
            //To do tomar el usuario en sesión
            _new.Author = 1;
            if (ModelState.IsValid)
            {
                EC_News ec_news = new EC_News();
                ec_news        = NewsViewModel.TranslateNewsViewModel(_new);
                ec_news.Author = 1;
                db.EC_News.Add(ec_news);

                try
                {
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (DbEntityValidationException ex)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (var failure in ex.EntityValidationErrors)
                    {
                        sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                        foreach (var error in failure.ValidationErrors)
                        {
                            sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                            sb.AppendLine();
                        }
                    }

                    throw new DbEntityValidationException(
                              "Entity Validation Failed - errors follow:\n" +
                              sb.ToString(), ex
                              );
                }
            }

            return(View(_new));
        }