Asked by:
Razor Pages: OnGet And OnPost Methods

Question
-
User-1590999572 posted
Visual Studio Code 1.48 | .NET Core 3.1
Hi all
Two Methods can be used to process Forms: Get and Post
In the Razor Pages, in the Archive; for example Index.cshtml.cs the structure is implemented:
public class IndexModel : PageModel { public void OnGet() { } }
As can be seen, it works exclusively with Razor Pages without MVC Controllers
1 ° How to implement a Form in the Index.cshtml File to be processed by this same page, obviously using the OnGet Method?
2 ° Is it the same procedure for the OnPost Method (method = "post") or is there an additional variant?
Thank you very much
Wednesday, September 23, 2020 5:21 PM
All replies
-
User-474980206 posted
try reading the docs for a basic example.
in razor pages, the view model and controller actions are merged into a page model. a razor page references a PageModel. so to add a form, add some model fields to hold data to your page model and create a razor page that references the IndexModel class, and create a form that binds to the IndexModel fields just like you would in a Razor View.
note: the PageModel class file is optional, you can define it in the Razor Page.
Wednesday, September 23, 2020 8:27 PM -
User1120430333 posted
For the page that has get and post methods, the <form method="post"> must be defined in the view.cshtml file for the page in order to post the form back to the post method on the page. I think that's what you're asking. .
Below is a link to Razor project out on Github you can examine. The folder in the project says Blazor but it's all Razor. You will see get/post on multiple Razor pages.
Wednesday, September 23, 2020 8:48 PM -
User-1590999572 posted
Bruce, Thanks for your reply, but ...
The Microsoft samples are very specific to a workbench, in addition to using their repositories on GitHub.
What I require is something simpler and more practical:
1º How to develop a simple Form to process Data that initially will not be entered into the Database but will be previously analyzed on the same page, for example Index, or on another, that is, retrieve and view the Data previously and if necessary apply certain processes to them before being entered into the Database?
2º How to establish and use the connection string to link with SQL Server 2014?
3º Is the Scaffolding Tool mandatory? How to implement this utility to customize it according to specific requirements?
Please remember that the work is exclusively with Razor Pages and not with the MVC Architecture (that is, there are no Controllers)
Thank you very much already
Wednesday, September 23, 2020 11:52 PM -
User1120430333 posted
Bruce, Thanks for your reply, but ...
The Microsoft samples are very specific to a workbench, in addition to using their repositories on GitHub.
What I require is something simpler and more practical:
1º How to develop a simple Form to process Data that initially will not be entered into the Database but will be previously analyzed on the same page, for example Index, or on another, that is, retrieve and view the Data previously and if necessary apply certain processes to them before being entered into the Database?
2º How to establish and use the connection string to link with SQL Server 2014?
3º Is the Scaffolding Tool mandatory? How to implement this utility to customize it according to specific requirements?
Please remember that the work is exclusively with Razor Pages and not with the MVC Architecture (that is, there are no Controllers)
Thank you very much already
MVC is not a architecture. MVC is a UI design pattern just like MVP and MVVM are UI design patterns that work with Windows desktop or ASP.NET Web UI(s) just like MVC works with either UI environment.
https://www.codeproject.com/Articles/383153/The-Model-View-Controller-MVC-Pattern-with-Csharp
https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP
Also, Razor page solution uses the MVC pipeline, and as far as a connectionstring is concerned, .NET Core is .NET Core. It makes no difference if it's a Windows desktop or ASP.NET Web based solution using .NET Core, it all works the same in using the basic functionality of the .NET Core framework such as basic usage on obtaining the connectionstring and many other basic functionalities of .NET Core. However, things are much simpler in ASP.NET Core where things are done for the developer as opposed to Windows desktop Core where a developer has to dig in and figure things out, becuase there are no examples for Windows Core desktop solutions like there are for ASP.NET Core solutions.
One can use Mr. Wizard, which are the scaffolding tools and other such do it for the developer tools, or one figures out how to do things manually never needing to use Mr. Wizard ever. Of course, one doesn't need to use Mr. Wizard in Windows desktop or ASP.NET Web solutions .NET Core or non Core if one chooses to do so, which is the only way to fly and the only way one really learns.
Thursday, September 24, 2020 4:17 AM