ASP.NET MVC
ViewData, ViewBag and TempData in ASP.NET MVC
ViewData, ViewBag and TempData in ASP.NET MVC
Hi Folks,
In this blog we are discussing about how to pass data from controller to view or controller to another controller.
In MVC data will be passed by using ViewData ViewBag and TempData .
their are some scenarios.
1. pass data from controller to view.
2. pass data from one action method to another action method in a controller.
3. pass data from one controller to another controller.
You can follow my previous blogs for the code we are following step by step.
https://webdevelopmentknowledge.blogspot.com/2020/10/routing-in-aspnet-mvc.html
ViewBag :
ViewBag is used to pass data from controller to View. It is dynamic type no typecast and no null check required when using ViewBag object. ViewBag scope is for the request from controller to View.
Controller:
View:
Output:
ViewData:
This is a dictionary object used to pass data from controller to View. For this typecast and null check is mandatory else you will end up in null exceptions.
ViewData scope is for the request from controller to View.
Post a Comment
2 Comments
Informative man.
ReplyDeleteI didn't know these.
I was just copy paste older code.
I learnt now. Thanks for posting.
Keep posting...
Thanks Mukesh. we will be posting more posts with more information. Thanks for reading.
Delete