Pass and Get Data by ViewBag in MVC


I am new in MVC. I want to pass data in ViewBag from the controller and Getting data from ViewBag in View. Please suggest me a better way to achieve this task.

Last updated:12/16/2017 1:55:22 AM

1 Answers

Harsh kumar srivastava
Harsh kumar srivastava

Hi Shaan,

            I tried to give you the better way to accomplish your task.

First, we go to Controller section. And write code for Pass data into ViewBag in a controller method.

 ViewBag.YourVariableName='Value you want to send from controller to view';

Now we go to View section in our MVC project and trying to get data of ViewBag. Here is the code for getting data of ViewBag,


I think its helpful for you...