Variable in View Sample in MVC

Variable in View Sample in MVC

Introduction

In this article we will see how to create a variable and get its value.

Step 1

Create a MVC project using the "Empty" template.

Right-click on "Controllers" and select "Add" >> "Controller…".

image.png

Step 2

Select "MVC 5 Controller - Empty" to add an empty controller.

Click on the "Add" button.

image.png

Step 3

Name the controller as in the following:

image.png

Step 4

This will create an action method that returns a view.

image.png

Step 5

Now we need to create a view.

Right-click on "Index" and select "Add View...".

image.png

Step 6

Name the view and select "Empty (without model)" as the template.

Click on the "Add" button.

image.png

Step 7

As you can see in the following code snippet, we create a variable using @ in the view, store some value in variable and get the value inside the same view.

image.png

Step 8

Run the project and you get the value stored in the variable.

image.png

<< Day 3

>> Day 5