Created
August 3, 2013 15:28
-
-
Save cleytonferrari/6146849 to your computer and use it in GitHub Desktop.
Exemplo de Controller para um crud simples em ASP .Net MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using TISelvagem.Aplicacao; | |
using TISelvagem.Dominio; | |
namespace TISelvagem.UI.Web.Controllers | |
{ | |
public class AlunoController : Controller | |
{ | |
// | |
// GET: /Aluno/ | |
public ActionResult Index() | |
{ | |
var appAluno = new AlunoAplicacao(); | |
var listaDeAlunos = appAluno.ListarTodos(); | |
return View(listaDeAlunos); | |
} | |
public ActionResult Cadastrar() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Cadastrar(Aluno aluno) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var appAluno = new AlunoAplicacao(); | |
appAluno.Salvar(aluno); | |
return RedirectToAction("Index"); | |
} | |
return View(aluno); | |
} | |
public ActionResult Editar(int id) | |
{ | |
var appAluno = new AlunoAplicacao(); | |
var aluno = appAluno.ListarPorId(id); | |
if (aluno == null) | |
return HttpNotFound(); | |
return View(aluno); | |
} | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Editar(Aluno aluno) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var appAluno = new AlunoAplicacao(); | |
appAluno.Salvar(aluno); | |
return RedirectToAction("Index"); | |
} | |
return View(aluno); | |
} | |
public ActionResult Detalhes(int id) | |
{ | |
var appAluno = new AlunoAplicacao(); | |
var aluno = appAluno.ListarPorId(id); | |
if (aluno == null) | |
return HttpNotFound(); | |
return View(aluno); | |
} | |
public ActionResult Excluir(int id) | |
{ | |
var appAluno = new AlunoAplicacao(); | |
var aluno = appAluno.ListarPorId(id); | |
if (aluno == null) | |
return HttpNotFound(); | |
return View(aluno); | |
} | |
[HttpPost, ActionName("Excluir")] | |
[ValidateAntiForgeryToken] | |
public ActionResult ExcluirConfirmado(int id) | |
{ | |
var appAluno = new AlunoAplicacao(); | |
appAluno.Excluir(id); | |
return RedirectToAction("Index"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment