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 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.
Note: For details about Form Post in MVC, please refer my article ASP.Net MVC: Form Submit (Post) example.
When the Create Button is clicked, an object of BasicInfo class along with values is stored in to Session object and a redirection is made to the FetchController Action method.
public class HomeController : Controller
{
// GET: Home
public ActionResult 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
{
// GET: Fetch
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Index(BasicInfo basic)
{
return View(basic);
}
}
View
Source View
Inside the View, in the very first line the BasicInfo class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – 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.
@model Pass_Data_To_Controller_MVC.Models.BasicInfo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.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 type="submit" value="Create" /></td>
</tr>
</table>
}
</body>
</html>
Destination View
Inside the View, in the very first line the BasicInfo class is declared as Model for the View.
The View consists of an HTML Table which is used for displaying the values of the properties of the BasicInfo class.
@model Pass_Data_To_Controller_MVC.Models.BasicInfo
@{
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