Refer to this link for first part,
There are basically four different methods,
- Using Request Object.
- Using Formcollection Class Object.
- Using Parameters.
- Using Model Class Object.
Using the Formal Parameters
Here, we will discuss the second two methods.
Name |
Value |
EmployeeName |
Mayank Sharma |
EmployeeNumber |
123456789 |
EmployeeAddress |
Mumbai, India |
Create an action method in the HomeController (Home.cs) that renders the view on the UI.
- [HttpPost]
- public ActionResult EmployeeResult(string EmployeeName, string EmployeeNumber, stringEmployeeAddress)
- {
-
- string name = Convert.ToString("EmployeeName");
- string number = Convert.ToString("EmployeeNumber");
- string address = Convert.ToString("EmployeeAddress");
- 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/>");
-
- }
It also gives the same output as previously.
Using the Model Class Object (Strongly Typed View)
- Add a class
Employee.cs
- public class EmployeeModel
- {
- public EmployeeName { get; set;}
- public EmployeeNumber { get; set;}
- public EmployeeAddress { get; set;}
- }
- Create an action method in HomeController.cs
- [HttpGet]
- public ActionResult EmployeeResult()
- {
- EmployeeModel obj = new EmployeeModel();
- return View(obj);
- }
- [HttpPost]
- public ActionResult EmployeeResult(EmployeeModel model)
- {
- StringBuilder strs = new StringBuilder();
- strs.Append("<b>Name:</b> " + model.EmployeeName + "<br/>");
- strs.Append("<b>Number:</b> " + model.EmployeeNumber + "<br/>");
- strs.Append("<b>Address :</b> " + model.EmployeeAddress + "<br/>");
- return View(model);
- }
View
- @model MvcApplication.Models.EmployeeModel
-
-
- @using (Ajax.BeginForm("EmployeeResult","Home", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "divResult" })) {
-
-
- <fieldset>
- <legend>Employee Result</legend>
- <div id="divResult"></div>
- <ol>
- <li> @Html.LabelFor(model => model.EmployeeName) @Html.TextBoxFor(model => model.EmployeeName)
- </li>
- <li> @Html.LabelFor(model => model.EmployeeNumber) @Html.TextBoxFor(model => model.EmployeeNumber)
- </li>
- <li> @Html.LabelFor(model => model.EmployeeAddress) @Html.TextBoxFor(model => model.EmployeeAddress)
- </li>
- </ol>
- <button type="submit">Submit</button> }
-
- </fieldset>
-
- @section scripts{
-
- @Scripts.Render("~/bundles/jqueryval")
- }
It also gives the same output.