Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to POST from one Page Handler to another?

I am working on a ASP.NET Core 2.0 project using Razor Pages (not MVC).

I have the following flow:

  1. User fills out form and POSTs
  2. This page's POST handler validates the info and returns Page() if there are issues. If not, handler saves data to database.
  3. From here, I want the handler to POST to a different page's POST handler with the validated and saved data from Step 2.

How do I POST to another page from within a page handler? Is this the appropriate way to do this kind of thing? The reason I don't want to RedirectToPage() is because I don't want the final page in the sequence to be navigable via GET. The final page should not be accessible via a direct link but should only return on a POST.

I thought about validating/saving the data and setting a boolean "IsValid" and returning the page, checking for that IsValid, and immediately POSTing to the final page via JS. However this feels dirty.

like image 910
S-Vuk Avatar asked Jan 03 '18 01:01

S-Vuk


People also ask

What is OnGet ()?

OnGet() corresponds to Archive. Since an empty OnGet() method has no return statement, the return type is void : public void OnGet() { } Within the method we can set values to properties, which can be displayed in the view page.

How do you call a method in razor pages?

Razor pages have handler-methods which are HTTP verbs. So to call a method from your page you need to put On followed by the http verb you want then your method name . And in your view pass the name to the asp-page-handler without the OnPost or OnGet prefix or Async suffix.

What is OnGetAsync?

When a request is made for the page, the OnGetAsync method returns a list of movies to the Razor Page. On a Razor Page, OnGetAsync or OnGet is called to initialize the state of the page. In this case, OnGetAsync gets a list of movies and displays them.


1 Answers

Set the "asp-page" property of the form to your other page. Then set values in the standard way.

<form method="post" asp-page="/pathto/otherpage">
Select Example:<select name="DataForOtherPage">

Then in your controller, bind the value...

 [BindProperty]
 public string DataForOtherPage { get; set; }
like image 88
pcalkins Avatar answered Oct 21 '22 13:10

pcalkins