Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Html.ActionLink from a button?

How can I call the "Register" ActionLink (The one generated on the standard MVC5 project using VS2013) from a bootstrap button?

This is the original code:

@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })

I tried this but obviously it didn't work:

<button type="button" class="btn btn-info" onclick="window.location.href('@Url.Action("Register", "Account")')">Add New User</button>
like image 613
Ben Junior Avatar asked Oct 17 '15 17:10

Ben Junior


People also ask

How do you link a button in HTML?

Using onclick Event: The onclick event attribute works when the user click on the button. When mouse clicked on the button then the button acts like a link and redirect page into the given location. Using button tag inside <a> tag: This method create a button inside anchor tag.

How do you pass an ActionLink model?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.

What is HTML ActionLink ()?

ActionLink(HtmlHelper, String, String, String, String, String, String, Object, Object) Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, URL fragment, route values, and HTML attributes.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


2 Answers

<input type="button" class="btn btn-info" value="Let's Go!" onclick="location.href='@Url.Action("Action", "Controller")'" />

This should work fine...

like image 163
Burk Avatar answered Sep 22 '22 17:09

Burk


@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { @class = "btn btn-primary", @role="button" })  
like image 33
David Kavalir Avatar answered Sep 18 '22 17:09

David Kavalir