Search id with Razor and Ajax ASP.NET

0

The detail is that in my view I have the delete and search mode. Through an ajax I do the search but it does not do the action, because it is looking for the view that it does not have and that is the same one where I have the method to eliminate and that only eliminates it and looks for it through ajax, someone that helps me look by means of id and paint the data in the fields.

Note: In the ajax if I change this name search by delete does not search delete because it looks for a view that does not exist:

@Url.Action("Buscar", "Alumno")'
    @model model.entity.Alumno    
    @{
        Layout = null;
        ViewBag.Title = "Eliminar";
    }

    <h2>Eliminar</h2>
    @using (Html.BeginForm("Eliminar", "Alumno", FormMethod.Post))
    {
        <div>
            <div>
                <text>ID</text>
                @Html.TextBoxFor(model => model.idAlumno)
            </div>
            <div>
                <text>Nombre</text>
                @Html.TextBoxFor(model => model.nombre)
            </div>
            <div>
                <text>Apellido</text>
                @Html.TextBoxFor(model => model.Apellido1)
            </div>
            <div>
                <text>Telefono</text>
                @Html.TextBoxFor(model => model.Telefono1)
            </div>
            <div>
                <text>Estado</text>
                @Html.TextBoxFor(model => model.estado)
            </div>
        </div>

        <button type="submit">Eliminar</button>

        <a href="#" id="bus">buscar</a>

    }


    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            $("#bus").click(function () {
                buscarAjax();

            });

            //busco un control atraves de jquery
            function buscarAjax() {
                $.ajax({
                    url: '@Url.Action("Buscar", "Alumno")',

                    data: { id: $("#idAlumno").val() },
                    type: "post",
                    cache: true,
                    success: function (retorno) {
                        //$("#contenido").html(retorno);
                        //$('td').highlight($("#idAlumno").val());
                        //$('h3').text('Se han encontrado ' + $('.highlight').length + ' coincidencias');
                        alert("A logrado la busqueda")
                    },
                    error: function () {
                        alert("Se ha producido un error");
                    }
                });
            }
        });
    </script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using model.Neg;
using model.entity;
using mdel.neg;



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

        private AlumnoNeg objAlumnoNeg;

        private AlumnoDao objAlumnoDao;

        #region Metodo Crear Alumno
        public AlumnoController()
        {
            objAlumnoNeg = new AlumnoNeg();
        }
        //el de arriba solo te va a abrir una pantalla donde se muestren los input para que el usuario meta datos
        public ActionResult Crear()
        {

            return View();
        }
        //el segundo es el post es cuando el usuario ya lleno los input y dio click al boton submit
        [HttpPost]
        public ActionResult Crear(Alumno objAlumno)
        {
            //objAlumnoNeg.crear(objAlumno);
            AlumnoDao ins = new AlumnoDao();
            ins.crear(objAlumno);
            return RedirectToAction("inicio");
        }
        #endregion

        #region Metodo Editar Alumno
        public ActionResult Editar(string id)
        {

            AlumnoDao ins = new AlumnoDao();
            Alumno busca = new Alumno();
            busca = ins.finds(id);
            return View(busca);


            //return View();
        }
        [HttpPost]
        public ActionResult Editar(Alumno objAlumno)
        {
            if (ModelState.IsValid)
            {
                AlumnoDao buscarnuevo = new AlumnoDao();
                buscarnuevo.Actualizar(objAlumno);

            }
            return RedirectToAction("inicio");
        }
        #endregion

        #region Metodo Eliminar
        public ActionResult Eliminar(string id)
        {
             AlumnoDao ins = new AlumnoDao();
            Alumno busca = new Alumno();
            busca = ins.finds(id);
            return View(busca);


            //return View();
        }
         [HttpPost]
        public ActionResult Buscar(string id)
        {
            AlumnoDao ins = new AlumnoDao();
            Alumno busca = new Alumno();
            busca = ins.finds(id);
            return View(busca);


            //return View();
        }
           [HttpPost]
        public ActionResult Eliminar(Alumno objAlumno)
        {
            if (ModelState.IsValid)
            {
                AlumnoDao Eliminar = new AlumnoDao();
                Eliminar.Eliminar(objAlumno);


            }
            return RedirectToAction("inicio");
        }
        #endregion

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

    }
}
    
asked by Chuy Mogollan 14.12.2018 в 01:18
source

0 answers