In this article I will explain with an example, how to pass (send) data from one Action method to another Action method in ASP Net Core (.Net Core 8) MVC.
Model
The following Model Class consists of two properties.
public class BasicInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
Controllers
Source Controller
The Controller consists of Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action method handles the Form Submission when the Create Button is clicked.
This Action method for POST operation accepts BasicInfo as parameter. The values posted from the Form inside the View are received through this parameter.
Finally, a redirection is made to the Index Action method of Fetch Controller along with the object of BasicInfo class.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(BasicInfo basic)
{
return RedirectToAction("Index", "Fetch", basic);
}
}
Destination Controller
The Controller consists of Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling GET operation
Inside this Action method, the BasicInfo class object is retrieved from the HomeController Action method and then it is sent to the View.
public class FetchController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Index(BasicInfo basic)
{
return View(basic);
}
}
View
Source View
The View consists of an HTML Form method with the following parameters.
asp--action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
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.
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<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 type="submit" value="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.
@{
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.Name</td>
</tr>
<tr>
<td>Age</td>
<td>@Model.Age</td>
</tr>
</table>
</body>
</html>
Screenshot
Downloads