In this article I will explain with an example, how to redirect to Action method with Model data in ASP.Net Core (.Net Core) MVC.
Model
The Model class consists of following properties.
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; }
}
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 Submit button is clicked which accepts PersonModel class object as parameter.
When the Submit Button is clicked, data from the View is received in the PersonModel class object.
Finally, the PersonModel class object is passed to the RedirectToAction method along with the name of the destination Controller and its Action method.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(PersonModel person)
{
return RedirectToAction("Index", "PersonDetails", person);
}
}
Destination Controller
The Controller consists of the following Action method.
Action method for handling GET operation
This Action method accepts the PersonModel class object as parameter.
Inside this Action method, the values from PersonModel class objects are received.
Note: The Model class object values are sent using
QueryString parameters in the URL.
public class PersonDetailsController : Controller
{
// GET: PersonDetails
public IActionResult Index(PersonModel person)
{
int personId = person.PersonId;
string name = person.Name;
string gender = person.Gender;
string city = person.City;
return View();
}
}
Views
Source View
HTML Markup
Inside the View, in the very first line the PersonModel 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.
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.
The Form consists of three HTML INPUT TextBoxes and an HTML SELECT element (DropDownList).
The Form also consists of a Submit button, which when clicked the Form is submitted.
@model Redirect_Model_Core.Models.PersonModel
@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">
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Person Details</th>
</tr>
<tr>
<td>PersonId: </td>
<td><input type="text" asp-for="PersonId" /></td>
</tr>
<tr>
<td>Name: </td>
<td><input type="text" asp-for="Name" /></td>
</tr>
<tr>
<td>Gender: </td>
<td>
<select asp-for="Gender" style="width:176px">
<option value="">Please select</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</td>
</tr>
<tr>
<td>City: </td>
<td><input type="text" asp-for="City" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Destination View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
</div>
</body>
Screenshots
The Source View
Model values received in the Destination Controller’s Action method
Downloads