Data Transfer Between View And Controller In ASP.NET MVC 5

In this article you will learn how to transfer data between View and Controller in ASP.NET MVC 5.

Introduction

This is my second article on ASP.NET MVC 5. In the first, we looked at the basics of ASP.NET MVC 5 and now we will move on the second article which briefly discuss the Models.

Step 1: Firstly, create an ASP.NET Project, then choose empty MVC and check the MVC checkbox. After that click OK and the project is created.

 create an ASP.Net Project

Step 2: Now add the controller on project. Simply left click on controller folder and then click on add. Choose 'MVC 5 Controller - Empty', give controller name HomeController and then click OK.

MVC5 Empty Controller

Step 3: Now add the class in model. Left click on model folder and add class; the default name of class is Class1. We replaced then name and named Employee.

add controller

class

Then we press Add button. Now our class is ready for coding.

Step 4: Now we make same properties in model class. After creating these properties press build button on visual studio to update the class. This done because often after creating the class it can’t be added in project. So build first, and don't type the complete property. Type pro and press tab button two times on keyboard and now the overall property is visible. Now change the data type.

properties in model

Step 5: Now move to the Controller section known as Homecontroller and create the action. Main benefit of creating view result is that when user fill the form after filling then it press submit button. After pressing submit you can see thank you; this message is located in View.

Controller section

Step 6: Left click on method and then click on Add View.

click on Add View

Step 7: After left click on Add View,  select Empty Template and Model class as Employee. Also, unmark the 'Use a layout page' .

Add View

Step 8: Now you can see we use razor syntax to include the C# syntax in current context.

Razor syntax

Step 9: Now make a new method in HomeController. When employee fills the form and press submit button then the thanks message is visible.

HomeController

Step 10: Now click on Thanks method and add View file similarly.

add View file

Add the heading in head body and after this save the program. Now run the program and fill the form to see the result.

Code

Read more articles on ASP.NET: