Search code examples
asp.net-mvcasp.net-mvc-3razorhtml.actionlink

@Html.ActionLink one works and one does not


I have two @Html.ActionLink's one works and one does not and I cannot figure out why?

They are both within the same controller: StoreController and both call ActionResults within different controllers, this is the one that works and calls the Index function within the Director Class:

<ul>
@foreach (var item in Model)
{
    <li>
    <h2>@item.Title</h2>
    </li>
    <li class = "lihead">
    Directed by 
    @Html.ActionLink((string)item.Director.Name, "Index", "Director", new { searchString = item.Director.Name }, null)
    </li>
    <li>
    <i>@item.Synopsis</i>
    </li> 
    <li class = "lihead">
    Price per ticket £
    @item.Price
    </li>        
}

This is the one that does not work:

 @foreach (var item in Model) {
        @Html.ActionLink("Check Availability", "CheckAvail", "Booking", new { id = item.ShowId })

}

This one calls within the StoreController when I want it to call the CheckAvail function within the BookingController. However it does work when I amend it as so:

 @foreach (var item in Model) {
        @Html.ActionLink("Check Availability", "CheckAvail", "Booking")}

But I need it to take through the ShowId to the function???


Solution

  • Use it like this

    @Html.ActionLink("Check Availability", "CheckAvail", "Booking", new { @id = item.ShowId },null)
    

    It is using this overload

    public static MvcHtmlString ActionLink(
        this HtmlHelper htmlHelper,
        string linkText,
        string actionName,
        string controllerName,
        Object routeValues,
        Object htmlAttributes
    )
    

    http://msdn.microsoft.com/en-us/library/dd504972.aspx

    the last parameter is the HTML attribute. If you have some HTML atribute to pass, you pass it there instead of null

    @Html.ActionLink("Check Availability", "CheckAvail", "Booking", new { @id = item.ShowId },new { @class="myCssClass"} )