W Y A T T F L E M I N G

Truth through experimentation

ASP.NET Core/MVC: Adding a Button to a Razor View Page that Returns a Method On-Click


Tags: C Sharp ASP.NET .NET Core ASP.NET MVC 5 Razor Pages

I needed to add a button in my ASP.NET Core MVC web application that on-click would start a method that would return a list of items scraped from another website.

Something like this:

The code I got to work for doing so is:


<button type="button" id="startButton" 
    onclick="location.href='@Url.Action('List', 'ArbitrageScraper')'">
    Start  Scrape
</button>

This code resides inside of the View for your controller. My button functions as a “Start” button for a web-scraping process.

The general formula for adding a button of your own is:

<button type="button" id="YOUR_BUTTONS_NAME" 
    onclick="location.href='@Url.Action('ACTION_METHOD_NAME',
     'CONTROLLER_NAME_WHERE_THE_ACTION_METHOD_RESIDES')'">
    TEXT YOU WANT TO DISPLAY ON THE BUTTON
</button>

My name is Wyatt Fleming. I am a Project Manager at WebJaguar and a life-long learner interested in history, economics, gardening, and computers.