0
votes

I built a web app using ASP.NET Razor Pages, and I am trying to run my progam.cs code when I click a button. Any ideas how I can do this?

I created the button and made sure that it worked, I just can figure out how to connect it to my code in program.cs

Index.cshtml http://google.com'" type="button"> Google

//Index.cshtml.cs public class IndexModel : PageModel { private readonly WebApplication2.Models.AppDbContext2 _context;

    public IndexModel(WebApplication2.Models.AppDbContext2 context)
    {
        _context = context;
    }

    public IList<Job> Job { get;set; }

    public async Task OnGetAsync()
    {
        Job = await _context.Job.ToListAsync();
    }
}

I have no results yet, but I would like the button to run some of the code in the main method, not all of it

1
Welcome to Stack Overflow. Are you trying to run a separate compiled .exe? It's unclear where this other code you want to run lives. The Introduction to Razor Pages is a good place to start if you've not done web programming before docs.microsoft.com/en-us/aspnet/core/razor-pages/…Brad Patton
@BradPatton Is it possible to have my button run an entire separate program compiled .exe?Benjamin Beuca
You can but it's generally a really bad idea - See stackoverflow.com/questions/48296629/…Brad Patton

1 Answers

0
votes

It is not typical that you would try to run code directly from program.cs in a web application. Normally you would use a pattern like MVC (Model, View, Controller) to handle information transfer. In the case of MVC your view would send information back to your controller. Then your controller could use that information however you would like. Heres a link to microsofts documentation on MVC in .net core. https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-2.2

Also it will help to familiarize yourself with the http verbs like GET, POST, PUT, DELETE and how you can use them. You can use HTML form elements to perform most of these actions. Heres a Link to a form Example. https://www.w3schools.com/html/html_forms.asp

Another way to handle sending the data back your controller would be using JavaScript. It is a bit more complicated than using a form but can be much more flexible. Heres a link to the javascript fetch API. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

and heres a quick example -

This is a simple form that links to my customer controller then activates the Location or date method.

<form action="Customer/Location">
    <input type="submit" class="SelectByLocation SelectBy" value="Location" />
</form>

<div id="CustApptTypeSelectScreen" class="SelectApptType">
    <form action="Customer/Location">
        <input type="submit" class="SelectByLocation SelectBy" value="Location" />
    </form>

    <form action="Customer/Date">
        <input type="submit" class="SelectByDate SelectBy" value="Date" />
    </form>

</div>

This is the controller its self with the Location() function. When you click Location Button it will go to that function and run it.

 public class CustomerController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
    public IActionResult Date()
    {
       return View("Date");
    }
    public IActionResult Location()
    {
        doSomethingFunction();
        return View("Location");
    }
}

I hope something here helps. -Good Luck.