locked
ActionLink to Details page linked by ID RRS feed

  • Question

  • User982203039 posted

    I am trying to add a actionlink to a details page. The link opens the page but with no results. Here is the detail of the  link:

    @Html.ActionLink(item.FirstName, "Details", "Employees", new { id = item.ID }, null)

    And the controller:

    public ActionResult Details(int id)
    {
    Employee employee = db.Employees.Find(id);
    if (employee == null)
    {
    return HttpNotFound();
    }
    return View(employee);
    }



    The link when hovering over it shows:Employees/Details/5

    Monday, June 17, 2019 2:53 PM

All replies

  • User475983607 posted

    Does an employee with ID 5 exist?  Can you share the View markup? 

    Monday, June 17, 2019 7:23 PM
  • User1520731567 posted

    Hi Baze72,

    @Html.ActionLink(item.FirstName, "Details", "Employees", new { id = item.ID }, null)

    And the controller:

    public ActionResult Details(int id)
    {//add a breakpoint on this line
    Employee employee = db.Employees.Find(id);
    if (employee == null)
    {
    return HttpNotFound();
    }
    return View(employee);
    }

    I suggest you could add breakpoint on highlight line,check if your id is passed over.

    If no,maybe there are some wrong in your route,check your RouteConfig.cs.

    if id has value,check if green line has value,

    if employee ==null,it do will return HttpNotFound;

    Best Regards.

    Yuki Tao

    Tuesday, June 18, 2019 7:46 AM
  • User753101303 posted

    Hi,

    "no results" that is? Do you mean it shows a 404 not found or maybe an empty view ? Either the view doesn't properly show data or maybe the employee for this id does have empty fields?

    Always start from what actually happens and then you read the code to understand why it happens (not to guess what is the thing that happens that you barrely described).

    Monday, June 24, 2019 10:46 AM