You are on page 1of 5

Passing Of Data From Controller To View

ViewData And ViewBag

This article deals with how we can pass data from a controller to view.For that we can use either
ViewData or ViewBag.
Let us see how we can implement it...

Adding Controller
1. First of all, we can add a control to the project as we already seen.

2. Give name to the controller and click on Add button.


Using ViewData

1. Double click on the controller and add the following contents to controller.

Adding View

Add a corresponding view for the controller.


1. By Double click on the view and add the following contents to view.

2. Saving and run the application, we will obtain the result. ie, it shows the current system date
and time.
Using ViewBag

1. Likewise we can also use ViewBag instead of ViewData.Same procedure repeat again.

2. Double click on the controller and add the following contents to controller.

3. Also by Double click on the view and add the following contents to view.
4. Then run the project and it will show the current system date and time.

You might also like