How to Pass data to view from Controller ?
There are three ways to pass data from controller to a view.
There are three ways to pass data from controller to a view.
View Model
View Bag
View Data
Let look at all one by one.
Sending data from View to Controller through View Model
First of all we have to define model in our view using @ model tag in cshtml file. Look in picture below I have passed a model named Movie and then using @ Model tag I have received that information.
Lets see how we have passed data from Controller.
2. Sending data from View to Controller through View Data
View Data works like dictionary we have to pass key and value. We set value against specific key in controller and an access it in view. Lets look through example.
Here it is controller side
In cshtml we can access data like this
3. Sending data from View to Controller through View Bag
View Bag is same like View Data with one difference that we don't need to set key in brackets. We can dynamically set any variable through this . Lets look through example. In controller we set data like this
In cshtml we can retrieve it like this
If you like my work you can help me in growing through following ways