In this article I will explain with an example, how to pass (send)
Session value from one Controller to another Controller in
ASP.Net Core (.Net Core 8) MVC.
Model
Following is a Model class named PersonModel with four properties i.e. PersonId, Name, Gender and City.
public class PersonModel
{
///<summary>
/// Gets or sets PersonId.
///</summary>
public int PersonId { get; set; }
///<summary>
/// Gets or sets Name.
///</summary>
public string Name { get; set; }
///<summary>
/// Gets or sets Gender.
///</summary>
public string Gender { get; set; }
///<summary>
/// Gets or sets City.
///</summary>
public string City { get; set; }
}
Namespace
You will need to import the following namespaces.
Controllers
Source Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, Session object is set.
Then string value is set in the
Session object using the
SetString method of the
HttpContext.Session property in Controller and the View is returned.
Action method for handling POST operation
This Action method handles the Form Submission when the Button is clicked.
When the
Send Button is clicked, the object of
PersonModel class is serialized into a
JSON string and then set into the
Session object using the
SetString method of the
HttpContext.Session property.
Finally, redirection is made to the PersonDetails Action method.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Send()
{
//Create the Model object.
PersonModel person = new PersonModel
{
PersonId = 1,
Name = "Mudassar Khan",
Gender = "Male",
City = "Mumbai"
};
//Serialize and set value in Session object.
HttpContext.Session.SetString("Person", JsonConvert.SerializeObject(person));
//Send Model object as Session to another Controller.
return RedirectToAction("Index", "PersonDetails");
}
}
Destination Controller
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, the
JSON string is fetched from the
Session object and deserialized back to the
PersonModel class object and finally it is sent to the View.
public IActionResult Index()
{
//Fetch the Serialized JSON string from Session.
PersonModel person = JsonConvert.DeserializeObject<PersonModel>(HttpContext.Session.GetString("Person"));
//Return Model to View.
return View(person);
}
Views
Source View
The View consists of an
HTML Form with following
ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Send.
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
The Form consists of a Submit Button and when the 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-action="Send" asp-controller="Home">
<input type="submit" value="Send" />
</form>
</body>
</html>
Destination View
Inside the View, in the very first line the PersonModel 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
PersonModel class.
@model Pass_Session_MVC_Core.Models.PersonModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Person Details</th>
</tr>
<tr>
<td>PersonId:</td>
<td>@Model.PersonId</td>
</tr>
<tr>
<td>Name:</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Gender:</td>
<td>@Model.Gender</td>
</tr>
<tr>
<td>City:</td>
<td>@Model.City</td>
</tr>
</table>
</body>
</html>
Screenshot
Downloads