How to create methods to delete, update, insert in ASP.NET controller

0

You can guide me, please so that I can make them Insert, Delete, Update methods, the question I am trying but it shows me that it has 0 arguments and sometimes it tells me that it has no overload of methods.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using model.Neg;
using model.entity;

namespace WebMVC4.Controllers
{
    public class AlumnoController : Controller
    {
        //
        // GET: /Alumno/

        private AlumnoNeg objAlumnoNeg;

        public AlumnoController() // Crear Alumno
        {
            objAlumnoNeg = new AlumnoNeg();
        }

        public ActionResult Crear(int id)
        {
            List<Alumno> list = objAlumnoNeg.crear();

            return View(list);
        }
        public ActionResult Eliminar()
        {

            List<Alumno> list = objAlumnoNeg.eliminar();

            if (list == null)
                return View("NotFound");
            else
                return View(list);
        }
        public ActionResult Actualizar()
        {

            var Acualizar = objAlumnoNeg.actualizar();
            return View(Acualizar);
        }

        public ActionResult inicio()
        {
            List<Alumno> list = objAlumnoNeg.findAll(); // Lista de Alumnos
            return View(list);
        }

    }
}
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using model.entity;

namespace mdel.neg
{
    public class AlumnoDao:obligatorio<Alumno>
    {
        private Conexion objConexion;
        private SqlCommand conn;

        public AlumnoDao()
        {
            objConexion = Conexion.SaberEstado();
        }

        public void crear(Alumno objAlumno)
        {
            string crear = "insert into Persona(idAlumno,nombre,Apellido,Telefono)values('" + objAlumno.IdAlumno + "'," + objAlumno.Nombre + "','" + objAlumno.Apellido1 + "','" + objAlumno.Telefono1 + "')";
            try
            {
                conn = new SqlCommand(crear, objConexion.getcon());
                objConexion.getcon().Open();
                conn.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                objConexion.getcon().Close();
                objConexion.cerrarConexion();

            }
        }
        public  void Actualizar(Alumno objAlumno)
        {
            string actualizar = "update Persona set nombre='" + objAlumno.IdAlumno + "',Apellido='" + objAlumno.Apellido1 + "',Telefono='" + objAlumno.Telefono1 + "' Where idAlumno ='" + objAlumno.IdAlumno + "'";
            try
            {
                conn = new SqlCommand(actualizar, objConexion.getcon());
                objConexion.getcon().Open();
                conn.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                objConexion.getcon().Close();
                objConexion.cerrarConexion();
            }
        }
        public void Eliminar(Alumno objAlumno)
        {
            string eliminar = "'delete from Persona where idAlumno ='" + objAlumno.IdAlumno + "'";

            try
            {
                conn = new SqlCommand(eliminar, objConexion.getcon());
                objConexion.getcon().Open();
                conn.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                objConexion.getcon().Close();
                objConexion.cerrarConexion();
            }
        }
        public bool find(Alumno objAlumno)
        {
            bool ExistenRegistros;
            string find = "select * from Persona where idAlumno = '" + objAlumno.IdAlumno + "'";
            try
            {
                conn = new SqlCommand(find, objConexion.getcon());
                objConexion.getcon().Open();
                SqlDataReader read = conn.ExecuteReader();
                ExistenRegistros = read.Read();
                if (ExistenRegistros)
                {
                    objAlumno.IdAlumno = Convert.ToInt32(read[0].ToString());
                    objAlumno.Nombre = read[1].ToString();
                    objAlumno.Apellido1 = read[2].ToString();
                    objAlumno.Telefono1 = read[3].ToString();

                    objAlumno.Estado = 99;

                }
                else
                {
                    objAlumno.Estado = 1;
                }
            }
            catch (Exception)
            {

                throw;
            }
            return ExistenRegistros;

        }
        public List<Alumno> findAll()
        {
            List<Alumno> LisAlumno = new List<Alumno>();

            string findAll = "select * from Persona";
            try
            {

                conn = new SqlCommand(findAll, objConexion.getcon());
                objConexion.getcon().Open();
                SqlDataReader read = conn.ExecuteReader();
                while (read.Read())
                {

                    Alumno objAlumno = new Alumno();
                    objAlumno.IdAlumno = Convert.ToInt32(read[0].ToString());
                    objAlumno.Nombre = read[1].ToString();
                    objAlumno.Apellido1 = read[2].ToString();
                    objAlumno.Telefono1 = read[3].ToString();
                    LisAlumno.Add(objAlumno);
                }
            }
            finally
            {
                objConexion.getcon().Close();
                objConexion.cerrarConexion();
            }
            return LisAlumno;
        }

                //}
                //catch (Exception)
                //{

                //    throw;

                //}           
                //return LisAlumno;

        public void actualizar(Alumno obj)
        {
            throw new NoNullAllowedException();
        }
        public void eliminar(Alumno obj)
        {
            throw new NoNullAllowedException();
        }
        public List<Alumno> finAll()
        {
            throw new NoNullAllowedException();
        }
        public void actualzar(Alumno obj)
        {
            throw new NotImplementedException();
        }
    }
}   
    
asked by Chuy Mogollan 07.12.2018 в 20:49
source

1 answer

0

You need to check for the subject of RESTful and its methods / verbs http.

Basically you need to be able to read information that is sent through your html form to the controlled, usually in a HttpPost (one of the RESTful verbs).

Here is an example:

    // GET: Person
    public ActionResult Index()
    {
        return View();
    }

    // GET: Person/Details/5
    public ActionResult Details(int id)
    {
        return View();
    }

    // GET: Person/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: Person/Create
    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    // GET: Person/Edit/5
    public ActionResult Edit(int id)
    {
        return View();
    }

    // POST: Person/Edit/5
    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add update logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    // GET: Person/Delete/5
    public ActionResult Delete(int id)
    {
        return View();
    }

    // POST: Person/Delete/5
    [HttpPost]
    public ActionResult Delete(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add delete logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
    
answered by 07.12.2018 в 23:40