Advertisement
Guest User

TarefasController.cs

a guest
Nov 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CadastroTarefas2.Models;
  7. using CadastroTarefas2.repositorio;
  8.  
  9. namespace CadastroTarefas2.Controllers
  10. {
  11.     public class TarefasController : Controller
  12.     {
  13.         // GET: Tarefas
  14.         private TarefaRepositorio tarefaRepositorio;
  15.  
  16.        
  17.         public ActionResult ObterTarefas()
  18.         {
  19.             tarefaRepositorio = new TarefaRepositorio();
  20.             ModelState.Clear();
  21.             return View(tarefaRepositorio.ObterTarefas());
  22.         }
  23.  
  24.         [HttpGet]
  25.         public ActionResult IncluirTarefa()
  26.         {
  27.             return View("ObterTarefas");
  28.         }
  29.  
  30.         [HttpPost]
  31.         public ActionResult IncluirTarefa(Tarefas tarefa)
  32.         {
  33.             try
  34.             {
  35.                 if (ModelState.IsValid)
  36.                 {
  37.                     tarefaRepositorio = new TarefaRepositorio();
  38.                     if (tarefaRepositorio.AdicionarTarefa(tarefa))
  39.                     {
  40.                         ViewBag.Mensagem = "Tarefa cadastrada com sucesso";
  41.                     }
  42.                 }
  43.                 return RedirectToAction("ObterTarefas");
  44.             }
  45.             catch (Exception)
  46.             {
  47.                 return View("ObterTarefas");
  48.             }
  49.         }
  50.  
  51.         [HttpGet]
  52.         public ActionResult EditarTarefa(int id)
  53.         {
  54.             tarefaRepositorio = new TarefaRepositorio();
  55.             return View(tarefaRepositorio.ObterTarefas().Find(t => t.ID == id));
  56.         }
  57.  
  58.         [HttpPost]
  59.         public ActionResult EditarTarefa(int id, Tarefas tarefa)
  60.         {
  61.             try
  62.             {
  63.                 tarefaRepositorio = new TarefaRepositorio();
  64.                 tarefaRepositorio.AtualizarTarefa(tarefa);
  65.                 return RedirectToAction("ObterTarefas");
  66.             } catch (Exception)
  67.             {
  68.                 return View("ObterTarefas");
  69.             }
  70.         }
  71.  
  72.         public ActionResult ExcluirTarefa(int id)
  73.         {
  74.             try
  75.             {
  76.                 tarefaRepositorio = new TarefaRepositorio();
  77.                 if (tarefaRepositorio.ExcluirTarefa(id))
  78.                 {
  79.                     //ViewBag.Mensagem = "Tarefa excluida com sucesso";
  80.                 }
  81.                 return RedirectToAction("ObterTarefas");
  82.             }
  83.             catch (Exception)
  84.             {
  85.                 return View("ObterTarefas");
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement