Home > Net >  Value cannot be null. Parameter name: entity error
Value cannot be null. Parameter name: entity error

Time:02-14

i have the view:

@model List<MVCCrud.Models.ViewModels.ListTablaViewModel>
@{
    ViewBag.Title = "Mis datos";
}

<h2>@ViewBag.Title</h2>

<div  style="text-align:right;padding:5px;">
    <a href="~/Tabla/Nuevo" >Nuevo</a>
</div>
<div >
    <div >
        <table >
            <tr>
                <th>#</th>
                <th>Nombre</th>
                <th>Correo</th>
                <th></th>
            </tr>
            @foreach (var oElemento in Model)
            {
                <tr>
                    <td>@oElemento.Id</td>
                    <td>@oElemento.Nombre</td>
                    <td>@oElemento.Correo</td>
                    <td>
                        <a  href="~/Tabla/Editar/@oElemento.Id">Editar</a>
                        <a  href="~/Tabla/Eliminar/@oElemento.Id">Eliminar</a>
                    </td>
                </tr>

            }
        </table>
    </div>

</div>

And the method Eliminar on the controller:

 [HttpDelete]
        public ActionResult Eliminar(int? cedula)
        {
            using (ClinicaEntities db = new ClinicaEntities())
            { 
           
                var oTabla = db.Pacientes.Find(cedula);
                db.Pacientes.Remove(oTabla);
                db.SaveChanges();
            }
            return Redirect("~/Paciente/");
        }

Once im trying to execute the method it gives me a "Value cannot be null. Parameter name: entity" error, its like its not passing the "cedula" to the method

CodePudding user response:

You can use @Url.Action to send your cedula field to the Controller method:

<a  href="@Url.Action("Editar","Tabla", new { cedula = oElemento.Id })">Editar</a>
<a  href="@Url.Action("Eliminar","Tabla", new { cedula = oElemento.Id })">Eliminar</a>
  • Related