Answered by:
Post the Model to the same page? in Razor Pages.

Question
-
User-1042970710 posted
Folks,
In ASP.NET Core Razor Pages, How I can Post the Model to the same page? as Page() method doesn't accept any model property
I want to set the values of the form on OnGet method and then post model to the same page.
Regards;
Imran
Tuesday, August 11, 2020 12:54 PM
Answers
-
User-821857111 posted
Leave the form's action as an empty string and don't apply an asp-page attribute. Then the form will be posted to itself. The OnGet method should return void, so there is no need for return Page() in that method. The OnPost handler will fire when the page is posted. That should return an IActionResult which allows you to return Page() in the event that ModelState is not valid, or redirect to another page on successful form submission and processing.
If you return Page() in OnPost, the model is implicitly passed. It is the PageModel for the page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 11, 2020 1:16 PM -
User711641945 posted
Hi jalali,
>I want to set the values of the form on OnGet method
Just set the property on your PageModel and set the value on OnGet method like below,then the razor pages could render the value:
Index.cshtml.cs:
public class IndexModel : PageModel { public string data { get; set; } public IActionResult OnGet() { data = "aaa"; return Page(); } }
Index.cshtml:
@page @model IndexModel <form method="post"> <input asp-for="data" /> <button>Submit</button> </form>
>and then post model to the same page.
If you want to post the data back to the backend,just add [BindProperty] attribtue:
[BindProperty] public string data { get; set; }
Result:
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 12, 2020 6:41 AM
All replies
-
User-821857111 posted
Leave the form's action as an empty string and don't apply an asp-page attribute. Then the form will be posted to itself. The OnGet method should return void, so there is no need for return Page() in that method. The OnPost handler will fire when the page is posted. That should return an IActionResult which allows you to return Page() in the event that ModelState is not valid, or redirect to another page on successful form submission and processing.
If you return Page() in OnPost, the model is implicitly passed. It is the PageModel for the page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 11, 2020 1:16 PM -
User711641945 posted
Hi jalali,
>I want to set the values of the form on OnGet method
Just set the property on your PageModel and set the value on OnGet method like below,then the razor pages could render the value:
Index.cshtml.cs:
public class IndexModel : PageModel { public string data { get; set; } public IActionResult OnGet() { data = "aaa"; return Page(); } }
Index.cshtml:
@page @model IndexModel <form method="post"> <input asp-for="data" /> <button>Submit</button> </form>
>and then post model to the same page.
If you want to post the data back to the backend,just add [BindProperty] attribtue:
[BindProperty] public string data { get; set; }
Result:
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 12, 2020 6:41 AM