Search code examples
c#asp.netasp.net-mvcmodel-view-controlleractionlink

asp.net, how can i go from localhost/Controller1/Index to localhost/Controller2/Details/{ID}?


I am trying to go from https://localhost:44383/Reservations/Index to https://localhost:44383/Bikes/Details/{Id} But I don't know what html actionlink i need.

I thought this was the right one: @Html.ActionLink("Details", "Details", "Bikes", new { Id = item.Reservation.Bike_Id })

Can someone tell me how to go from Reservation controller to details controller and give details and id as parameter?

@Html.ActionLink("Details", "Details" , "Bikes") brings me to localhost/Bikes/Details. But I need the id at the back as well

    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Bike bike = db.Bikes.Find(id);
        if (bike == null)
        {
            return HttpNotFound();
        }
        return View(bike);
    }

Solution

  • Have you tried

    @Html.ActionLink("Details", "Bikes", "Details", new { id  = item.Reservation.Bike_Id }, null)
    

    Note that I've used a lowercase 'i' in the id parameter passed. I also think you have the controller name and the action name the wrong way around here.

    Also worth noting that as you are passing parameters, you should use a null overload to correct the action link.