asp.net-mvcmodel-view-controllerparameters

My view actionlink does not send a parameter


I have this actionlink in my "cursos" index view(which redirects to another controller)

@Html.ActionLink("Ver Alumnos", "Index", "Usuarios", new { id= item.idCurso}, null)

in my controller I have this

        public ActionResult Index(int? idCursos)
    {
        var usuarios = db.Usuario.ToList();
        if(!(idCursos == null))
        {
            var query = from Usuario in db.Usuario
                        from Relaciones in db.Relaciones
                        where Relaciones.Cursos.idCurso == idCursos
                        select new Usuario
                        {
                            Nombre = Usuario.Nombre,
                            Apellido = Usuario.Apellido,
                            DNI = Usuario.DNI,
                        };
            usuarios = query.ToList();
            
        }
       

the problem is that it never enters that because the idCursos is always null, even though my link route in the browser looks like this

http://localhost:54680/Usuarios/Index/3

Thanks


Solution

  • The action parameter name must match the parameter passed to the actionlink, so in your case

      public ActionResult Index(int? id)
    

    should take care of it

    Update: I just noticed your URL, it is likely that your routing is configured to map to "id" parameter by default (if you haven't changed the default VS template).So even though the above correction works ... it is because the framework is expecting to map to the parameter "id" as configured in the routing.