Pass Data From View To Controller In ASP.NET - Part Two

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.

  1. [HttpPost]   
  2. public ActionResult EmployeeResult(string EmployeeName, string EmployeeNumber, stringEmployeeAddress)  
  3. {  
  4.   
  5.    string name = Convert.ToString("EmployeeName");  
  6.    string number = Convert.ToString("EmployeeNumber");  
  7.    string address = Convert.ToString("EmployeeAddress");  
  8.    StringBuilder strs = new StringBuilder();   
  9.    strs.Append("<b>Name:</b> " + name + "<br/>");   
  10.    strs.Append("<b>Number:</b> " + number + "<br/>");   
  11.    strs.Append("<b>Address :</b> " + address + "<br/>");   
  12.   
  13. }  
It also gives the same output as previously.

Using the Model Class Object (Strongly Typed View) 
  1. Add a class

    Employee.cs
    1. public class EmployeeModel  
    2. {  
    3.    public EmployeeName { getset;}   
    4.    public EmployeeNumber { getset;}   
    5.    public EmployeeAddress { getset;}   
    6. }  
  2. Create an action method in HomeController.cs
    1. [HttpGet]  
    2. public ActionResult EmployeeResult()  
    3. {  
    4.     EmployeeModel obj = new EmployeeModel();  
    5.     return View(obj);  
    6. }  
    7. [HttpPost]  
    8. public ActionResult EmployeeResult(EmployeeModel model)  
    9. {  
    10.     StringBuilder strs = new StringBuilder();  
    11.     strs.Append("<b>Name:</b> " + model.EmployeeName + "<br/>");  
    12.     strs.Append("<b>Number:</b> " + model.EmployeeNumber + "<br/>");  
    13.     strs.Append("<b>Address :</b> " + model.EmployeeAddress + "<br/>");  
    14.     return View(model);  
    15. }  

View

  1. @model MvcApplication.Models.EmployeeModel   
  2.   
  3.   
  4. @using (Ajax.BeginForm("EmployeeResult","Home"new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "divResult" })) {  
  5.   
  6.   
  7. <fieldset>  
  8.     <legend>Employee Result</legend>  
  9.     <div id="divResult"></div>  
  10.     <ol>  
  11.         <li> @Html.LabelFor(model => model.EmployeeName) @Html.TextBoxFor(model => model.EmployeeName)   
  12.         </li>  
  13.         <li> @Html.LabelFor(model => model.EmployeeNumber) @Html.TextBoxFor(model => model.EmployeeNumber)   
  14.         </li>  
  15.         <li> @Html.LabelFor(model => model.EmployeeAddress) @Html.TextBoxFor(model => model.EmployeeAddress)   
  16.         </li>  
  17.     </ol>  
  18.     <button type="submit">Submit</button> }   
  19.   
  20. </fieldset>   
  21.   
  22. @section scripts{  
  23.   
  24.     @Scripts.Render("~/bundles/jqueryval")   
  25. }  
It also gives the same output.