0

Looking for a way to call a c# function with a button inside one of my views? This seems like something trivial, but I haven't been able to find a solution for this. Any help is appreciated!

This is kinda how I expected it to be implemented, that or a @Html.Button

<input type="button" value="Sign Out" onclick="@SignOut()"/>
Pinball125
  • 25
  • 1
  • 4
  • Did you forget how web browsers and the client/server model work? If you want to execute server side code, you'll need to send an HTTP request to trigger it. Which means either a form submit, or AJAX etc. – mason Jun 12 '17 at 16:43
  • Or a JS function that redirects to the Action's URL subscribed to the `onClick` event. Not the most ellegant solution, though. – Reynevan Jun 12 '17 at 16:50

1 Answers1

2

There is no simple way of dynamically executing server function inside the Razor views.

You can however using button redirect the user to another route that will execute the server function.

Using the @Html.ActionLink("your text", "actionName", "controllerName") Razor helper.

Doing so you will create an element that on click will redirect the user to the specified route.

H. Bloch
  • 448
  • 5
  • 12