Caverman
Caverman

Reputation: 3707

Why Html.ActionLink isn't working in Bootstrap Modal?

I've got a work around but for my own understanding I'm trying to figure out why an @Html.ActionLink wouldn't work in a Bootstrap Modal?

I created a LogOut controller that does some logging and cleans up some session variables before redirecting. I was able to create a log out link on my Layout page using an @Html.ActionLink without issue. However, I also have a View that uses a Bootstrap Modal on it. When I use the same @Html.ActionLink it doesn't work.

@Html.ActionLink("OK", "Index", "LogOut", new { @class = "btn btn-success btn-lg" })

When I look at the F12 tools I can see the link is not correct. This is what it created.

<a class="btn btn-success btn-lg" href="/?Length=6">OK</a>

However, if I use a @Url.Action in an anchor tag instead it works fine. This is what works.

<a href='@Url.Action("Index","LogOut")' class="btn btn-success btn-lg">OK</a>

I'm okay with using the @Url.Action but I'm just trying to learn why the @Html.ActionLink wouldn't work.

Upvotes: 0

Views: 1065

Answers (1)

Shyju
Shyju

Reputation: 218722

You are using the ActionLink overload wrong!

Try this overload.

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

The 4th parameter is for passing route values(querystring values) and 5th is for html attributes of the link.

@Html.ActionLink("OK", "Index", "LogOut", null, new { @class = "btn btn-success btn-lg" })

Upvotes: 3

Related Questions