There are basically four different methods:
- Using Request Object.
- Using Formcollection Class Object.
- Using Parameters.
- Using Model Class Object.
Here, we will discuss the first two methods.
Using Request Object.
Here, we will use Name-Value pair technique.
Name |
Value |
EmployeeName |
Mayank Sharma |
EmployeeNumber |
123456789 |
EmployeeAddress |
Mumbai, India |
We have three pieces of data in Name-Value pairs. So we can access these data in a POST method by passing the Name as an indexer in the Request and getting the values.
Create an action method in the HomeController (Home.cs) that renders the view on the UI.
- [HttpGet]
- public ActionResult EmployeeResult()
- {
- return View();
- }
- [HttpPost]
- public ActionResult EmployeeResult()
- {
- string name = Request["EmployeeName"].ToString();
- string number = Request["EmployeeNumber"].ToString();
- string address = Request["EmployeeAddress"].ToString();
- StringBuilder strs = new StringBuilder();
- strs.Append("<b>Name:</b> " + name + "<br/>");
- strs.Append("<b>Number:</b> " + number + "<br/>");
- strs.Append("<b>Address :</b> " + address + "<br/>");
- }
View
- <fieldset>
- <legend>Employee Result</legend>
- @using (Ajax.BeginForm("EmployeeResult","Home", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "divResult" })) {
- <div id="divResult">
- </div>
- <ol>
- <li> @Html.Label("Name") @Html.TextBox("EmployeeName")
- </li>
- <li> @Html.Label("Number") @Html.TextBox("EmployeeNumber")
- </li>
- <li> @Html.Label("Address") @Html.TextBox("EmployeeAddress")
- </li>
- </ol>
- <button>Submit</button> }
- </fieldset>
Here is the Output.
Name
|
Mayank Sharma |
Number
123456789 |
Address
Mumbai,India |
Using Form collection Class Object
- [HttpPost]
- public ActionResult EmployeeResult(FormCollection frmObj)
- {
- string name = frmObj["EmployeeName"].ToString();
- string number = frmObj["EmployeeNumber"].ToString();
- string address = frmObj["EmployeeAddress"].ToString();
- StringBuilder strs = new StringBuilder();
- strs.Append("<b>Name:</b> " + name + "<br/>");
- strs.Append("<b>Number:</b> " + number + "<br/>");
- strs.Append("<b>Address :</b> " + address + "<br/>");
- }
Rest View will be the same.