In this article I will explain with an example, how to pass (send) Model data to Action method with data using QueryString Parameters in ASP.Net 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 following 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.
When the Submit button is clicked, an object of PersonModel class is initialized and its properties are set.
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
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Send()
{
PersonModel person = new PersonModel
{
PersonId = 1,
Name = "Mudassar Khan",
Gender = "Male",
City = "Mumbai"
};
//Send Model object in QueryString to another Controller.
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 which finally returned to the View.
Note: The Model class object values are sent using QueryString parameters in the URL.
public class PersonDetailsController : Controller
{
// GET: PersonDetails
public ActionResult Index(PersonModel person)
{
return View(person);
}
}
Views
Source View
HTML Markup
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.
The Form consists of a Submit button, which when clicked the Form is submitted.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Send", "Home", FormMethod.Post))
{
<input type="submit" value="Send" />
}
</body>
</html>
Destination 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 Table which is used for displaying the values of the properties of PersonModel class.
@model Pass_Data_QueryString_MVC.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