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()"/>
2
  • 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. Commented Jun 12, 2017 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. Commented Jun 12, 2017 at 16:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Plus, you can add some css styling to it to make it look like a button.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.