In this article I will explain with an example, how to pass (send) data from one Handler method to another Handler method in ASP.Net (.Net Core 8) Razor Pages.
Model
The Model class consists of following properties.
public class BasicInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
Index PageModel (Code-Behind)
Source PageModel
The PageModel consists of following Handler methods.
Handler method for handling GET operation
This Handler method left empty as it is not required.
Handler method for handling POST operation
This Handler method handles the Form Submission when the Create Button is clicked.
This Handler method for POST operation accepts BasicInfo as parameter. The values posted from the Form inside the Razor Page are received through this parameter.
Finally, a redirection is made to the Fetch Handler method along with the object of BasicInfo class.
public class IndexModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPostCreate(BasicInfo basic)
{
return new RedirectToPageResult("Fetch", basic);
}
}
Destination PageModel
The PageModel consists of following Handler methods.
Handler method for handling GET operation
Inside this Handler method, the BasicInfo class object is retrieved from the IndexModel Handler method and then it is set to the public property.
public class FetchModel : PageModel
{
public BasicInfo BasicInfo{ get; set; }
public void OnGet(BasicInfo basic)
{
this.BasicInfo = basic;
}
}
Razor Page (HTML)
HTML Markup
Source View
Inside the Razor Page, first the TagHelpers is inherited.
The Razor Page consists of an HTML Form which has been created using the following TagHelpers attribute.
Method - It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the HTML Form there are two input field created for capturing value for Name and Age and a Submit Button.
When the Submit Button is clicked, the Form is submitted.
Note: In the Razor PageModel, the Handler method name is OnPostCreate but here it will be specified as Create when calling from the Razor HTML Page.
@page
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@model Pass_Data_Handler_Method_Core_Razor.Pages.IndexModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post">
<h4>Basic Info</h4>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="Age" /></td>
</tr>
<tr>
<td></td>
<td><input id="btnSubmit" type="submit" value="Create" asp-page-handler="Create" /></td>
</tr>
</table>
</form>
</body>
</html>
Destination View
The View consists of an HTML Table which is used for displaying the values of the properties of the BasicInfo class.
@page
@model Pass_Data_Handler_Method_Core_Razor.Pages.FetchModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>@Model.BasicInfo.Name</td>
</tr>
<tr>
<td>Age</td>
<td>@Model.BasicInfo.Age</td>
</tr>
</table>
</body>
</html>
Screenshot
Downloads