コード例 #1
0
ファイル: LivroController.cs プロジェクト: andrebaltieri/5502
        public ActionResult Create(EditorBookViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var categorias = _db.Categorias.ToList();
                model.CategoriaOptions = new SelectList(categorias, "Id", "Nome");
                return View(model);
            }

            var livro = new Livro();
            livro.Nome = model.Nome;
            livro.ISBN = model.ISBN;
            livro.DataLancamento = model.DataLancamento;
            livro.CategoriaId = model.CategoriaId;
            _db.Livros.Add(livro);

            try
            {
                throw new Exception("Falha no banco");
                _db.SaveChanges();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Mensagem", ex.Message);
                var categorias = _db.Categorias.ToList();
                model.CategoriaOptions = new SelectList(categorias, "Id", "Nome");
                return View(model);
            }

            return RedirectToAction("Index");
        }
コード例 #2
0
ファイル: LivroController.cs プロジェクト: andrebaltieri/5502
        public ActionResult Create()
        {
            var categorias = _db.Categorias.ToList();
            var model = new EditorBookViewModel
            {
                Nome = "",
                ISBN = "",
                CategoriaId = 0,
                CategoriaOptions = new SelectList(categorias, "Id", "Nome")
            };

            return View(model);
        }
コード例 #3
0
ファイル: LivroController.cs プロジェクト: andrebaltieri/5502
        public ActionResult Edit(int id)
        {
            var categorias = _db.Categorias.ToList();
            var livro = _db.Livros.Find(id);

            var model = new EditorBookViewModel
            {
                Nome = livro.Nome,
                ISBN = livro.ISBN,
                CategoriaId = livro.CategoriaId,
                CategoriaOptions = new SelectList(categorias, "Id", "Nome")
            };

            return View(model);
        }
コード例 #4
0
ファイル: LivroController.cs プロジェクト: andrebaltieri/5502
        public ActionResult Edit(EditorBookViewModel model)
        {
            var livro = _db.Livros.Find(model.Id);
            _db.Entry<Livro>(livro).State = System.Data.Entity.EntityState.Modified;
            _db.SaveChanges();

            return RedirectToAction("Index");
        }