thiagoluiz / CrudModalAspnetCoreMVC

Crud Modal em ASP.NET MVC e Firebird

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crud Modal Usando C# ASP.NET Core MVC

Crud Modal em C# ASP.Net Core MVC# Exemplo de um Crud Modal utilizando ASP.NET Core MVC

Proposta

Criar um crud onde as views serão chamadas de forma modal, neste exemplo utilizei o banco Firebird.

Primeiramente definimos o código javascript onde sera feita a chamada modal em cada clique do botão

<script>
    $(function () {
        $(".details").click(function () {
            var id = $(this).attr("data-id");
            $("#modal").load("/Paciente/Details?id=" + id, function () {
                $("#modal").modal();
            })
        });

        $(".edit").click(function () {
            var id = $(this).attr("data-id");
            $("#modal").load("/Paciente/Edit?id=" + id, function () {
                $("#modal").modal();
            })
        });

        $(".delete").click(function () {
            var id = $(this).attr("data-id");
            $("#modal").load("/Paciente/Delete?id=" + id, function () {
                $("#modal").modal();
            })
        });

        $(".create").click(function () {
            $("#modal").load("/Paciente/Create", function () {
                $("#modal").modal();
            })
        });

    })
</script> 

Com isso, no nosso controller atráves de requisições POST obtemos os dados vindo de cada tela. Exemplo:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(include: "IdPaciente,Nome,Idade,Peso,Altura")] Paciente paciente)
        {
            if (ModelState.IsValid)
            {
                PacienteDAL pd = new PacienteDAL();
                pd.InserirPaciente(paciente);
                return RedirectToAction("Index","Paciente");
            }


            return View(paciente);
        }
        
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(include: "IdPaciente,Nome,Idade,Peso,Altura")] Paciente paciente)
        {
            if (ModelState.IsValid)
            {
                PacienteDAL pd = new PacienteDAL();
                pd.EditarPaciente(paciente);
                return RedirectToAction("Index");
            }
            return View(paciente);
        }        
        
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            PacienteDAL pd = new PacienteDAL();
            pd.DeletarPaciente(id);
            return RedirectToAction("Index");
        }        

About

Crud Modal em ASP.NET MVC e Firebird


Languages

Language:HTML 47.5%Language:C# 28.5%Language:JavaScript 21.2%Language:CSS 2.8%