2

Im working in a C# MVC .Net web project, and I used an ActionLink with a parameter, but I cant read it into the Controller.

Actionlink:

@model IEnumerable<FINAL.Models.Medico>

@foreach (var item in Model) 
{
    @Html.ActionLink("Eliminar", "DeleteMedico", new { id = item.NOMBRE.ToString() })
}

And heres how im trying to read it:

public ActionResult DeleteMedico(String elmedico) 
{
    //This WRITE is returning null. :(
    System.Diagnostics.Debug.WriteLine("ELMEDICO:" + elmedico);
    return View();    
}

Im pretty new in MVC .NET development so I need your help.

Thanks.

David L
  • 32,885
  • 8
  • 62
  • 93
Karlo A. López
  • 2,548
  • 3
  • 29
  • 56

1 Answers1

5

Your parameter is improperly named based on your controller's expected arguments.

Either rename the parameter being passed:

@Html.ActionLink("Eliminar", "DeleteMedico", new { elmedico = item.NOMBRE.ToString() })

Or rename the controller's argument to id:

public ActionResult DeleteMedico(String id) 
{
    System.Diagnostics.Debug.WriteLine("ELMEDICO:" + id);
    return View();
}
David L
  • 32,885
  • 8
  • 62
  • 93
  • Thanks, I now understand this part of the syntax, only one thing... It didint worked at first, I needed to add a `null` in `new { elmedico = item.NOMBRE.ToString() }, null` <-- like this, and then, It worked. Why is this? Thanks! – Karlo A. López Jun 11 '15 at 17:22
  • And also, I cant pass the whole `item` object? because when i do it I receive NULLReferenceException when trying to use it in code. – Karlo A. López Jun 11 '15 at 17:30
  • The null was probably required to select the correct overload of `ActionLink`. As for the item, it's not advisable to pass the entire object since it would all end up on the query string. – David L Jun 11 '15 at 18:25