Skip to content

Instantly share code, notes, and snippets.

@cleytonferrari
Created August 3, 2013 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleytonferrari/6146849 to your computer and use it in GitHub Desktop.
Save cleytonferrari/6146849 to your computer and use it in GitHub Desktop.
Exemplo de Controller para um crud simples em ASP .Net MVC
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