Search code examples
asp.netasp.net-corerazor

RazorPage not calling PageModel function


I can't get a button to call a function in the PageModel

<button type="button" class="btn btn-secondary" asp-page-handler="ExecuteScraping" asp-route-servername="@(server.serverName)">Update All</button>
public async Task<IActionResult> OnGetExecuteScrapingAsync(string servername)
            {
                await _controller.ExecuteScraping(servername);
                return RedirectToPage("Index");
            }

Also tried function names such as:

ExecuteScraping()
ExecuteScrapingAsync()

Seems like such an easy problem but can't get these 'other' functions thats not OnPostX or OnGetX to work.

Using .Net 5


Solution

  • You can try to use <a> tag.So that when click it will send a get request:

    <a asp-page-handler="ExecuteScraping" asp-route-servername="@(server.serverName)">Update All</a>
    

    If you want to send a post request,you can try to use form:

    <form method="post">
        <button type="submit" class="btn btn-secondary" asp-page-handler="ExecuteScraping" asp-route-servername="testName">Update All</button>
    </form>
    

    handler:

    public async Task<IActionResult> OnPostExecuteScrapingAsync(string servername)
                {
                    await _controller.ExecuteScraping(servername);
                    return RedirectToPage("Index");
                }