0

I have a very simple issue in my asp.net core mvc project.

I have a button in one of my views, and when this button is clicked, I want to redirect to a specific controller, and a action in this controller.

Currently it looks like this:

<button  class="dropbtn" class="glyphicon glyphicon-user"> something</button>

I've been looking around for methods of doing this, and one of the solutions I found, was the @Html.ActionLink("pik", "Index", "Home").

So I tried:

 <button onclick="@Html.ActionLink("pik", "Index", "Home")" class="dropbtn" class="glyphicon glyphicon-user"> Log in </button>

However this is just displayed as raw html. Does'nt work.

Then, here I found this:

<button nclick="location.href='<%: Url.Action("Action", "Controller") %>'" class="dropbtn" class="glyphicon glyphicon-user"> Log in

However, my IDE just gives a bunch of unexpected token in this line. I don't undestand it either.

I then found this:

@using (Html.BeginForm("Index", "Home", FormMethod.Post)){

  <button type="submit" class="btn btn-default" class="dropbtn" class="glyphicon glyphicon-user">Submit</button>
}
                                     

This works in terms of the button, but for some reason, when I wrap the button in a form, my styling gets messed up.

I would rather not use javascript for redirecting.

So how can I redirect to a controller action with a button?

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
Grazosi
  • 603
  • 1
  • 10

1 Answers1

0

@Html.ActionLink() creates an html link with an <a> tag. If you are using Bootstrap (highly recommended) you can use this.

<a href='@Url.Action("Index", "Home")' class="glyphicon glyphicon-user">Click Me</a>

or if you want to use helpers, like this

@Html.ActionLink("pik", "Index", "Home", new { @class = "glyphicon glyphicon-user" })

This is the cleanest way I know of to create button with a link.

You can also check this out.

cytek04
  • 472
  • 5
  • 17