Form Data Submission And Displaying Data In Another View Using MVC

In this article we are going to discuss about form data submission using @Html.BeginForm Helper method and passing the data to another view.

In this article we are using Tempdata for passing data between different controllers.

The main advantage of TempData is passing data between different controllers when redirect happens it retains data between controller to controller.

Firstly, we are going to create the MVC Solution

Select Empty Template and add MVC folder reference,

Referance

Add new Controller in Controllers folder,

Folder

Select MVC 5 Controller - Empty,

Empty

Give Conroller name as Home,

Home

Add a View

Right click on the Action Name and add view,

add

add

Create Model class in Model folder,

  1. public class Employee  
  2. {  
  3.     public string FirstName   
  4.   {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string LastName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string Salary   
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.   
  19. }  
Controller Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Model_Binding_in_Diffrent_Ways.Models;  
  7. namespace Model_Binding_in_Diffrent_Ways.Controllers   
  8. {  
  9.     public class HomeController: Controller   
  10.     {  
  11.         // GET: Home  
  12.         public ActionResult Index()   
  13.         {  
  14.             Employee emp = new Employee();  
  15.             return View(emp);  
  16.         }  
  17.   
  18.         [HttpPost]  
  19.         public ActionResult SubmitEmp(Employee emp)  
  20.         {  
  21.             TempData["Emp"] = emp;  
  22.             return Redirect("/Home/EmpDetails");  
  23.         }  
  24.   
  25.         public ActionResult EmpDetails()  
  26.         {  
  27.             Employee emp = TempData["Emp"as Employee;  
  28.             return View(emp);  
  29.         }  
  30.     }  
  31. }  
code

Index View Code
  1. @model Model_Binding_in_Diffrent_Ways.Models.Employee  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. }  
  5. @Html.BeginForm("SubmitEmp""Home",FormMethod.Post)  
  6. {  
  7. <table class="table table-responsive table-bordered" >  
  8. <tr>  
  9. <td>First Name</td>  
  10. <td>  
  11. @Html.TextBox("FirstName" ,null,htmlAttributes: new { @class = "form-control" })  
  12.   
  13. </td>  
  14. </tr>  
  15. <tr>  
  16. <td>Last Name</td>  
  17. <td>  
  18. @Html.TextBox("LastName"null, htmlAttributes: new { @class = "form-control" })  
  19.   
  20. </td>  
  21. </tr>  
  22. <tr>  
  23. <td>Salary</td>  
  24. <td>  
  25. @Html.TextBox("Salary"null, htmlAttributes: new { @class = "form-control" })  
  26.   
  27. </td>  
  28. </tr>  
  29. <tr>  
  30. <td colspan="2" align="right">  
  31. <input type="submit" value="Submit" class="btn btn-success" />  
  32. </td>  
  33. </tr>  
  34. </table>  
  35. }  
code

Run the application, provide the values and submit the form,

Form

EmpDetails View
  1. @model Model_Binding_in_Diffrent_Ways.Models.Employee  
  2. @ {  
  3.     ViewBag.Title = "EmpDetails";  
  4. }  
  5.   
  6. @if(Model != null) { < div >  
  7.         Employee Deatils < /div>  
  8.   
  9.     < div >  
  10.         First Name: @Model.FirstName < /div>  
  11.   
  12.     < div >  
  13.         Last Name: @Model.LastName < /div>  
  14.   
  15.     < div >  
  16.         Salary: @Model.Salary < /div>  
  17. else { < div > Employee Deatils not found < /div>  
  18. }  
code
The Output of the EmpDetails View,

Output