58
37
|
Could any body explain, when to use
I have a requirement, where I need to set a value in a controller one, that controller will redirect to Controller Two and Controller Two will render the View. I have tried to use ViewBag, the value gets lost by the time I reach Controller Two. Can I know when to use and advantages or disadvantages? Thanks |
||||||||||||||||||||
|
105
|
Allows you to store data that will survive for a redirect. Internally it uses the Session as baking store, it's just that after the redirect is made the data is automatically evicted. The pattern is the following:
Allows you to store data in a controller action that will be used in the corresponding view. This assumes that the action returns a view and doesn't redirect. Lives only during the current request. The pattern is the following:
and in the view:
or with ViewData:
and in the view:
This being said, none of those two constructs should ever be used. You should use view models and strongly typed views. So the correct pattern is the following: View model:
Action:
Strongly typed view:
After this brief introduction let's answer your question:
and the corresponding view (
There are drawbacks of using TempData as well: if the user hits F5 on the target page the data will be lost. Personally I don't use TempData neither. It's because internally it uses Session and I disable session in my applications. I prefer a more RESTful way to achieve this. Which is: in the first controller action that performs the redirect store the object in your data store and user the generated unique id when redirecting. Then on the target action use this id to fetch back the initially stored object:
The view stays the same. |
||||||||||||||||||||||||||||||||
|
5
|
ASP.NET MVC offers us three options ViewData, ViewBag, and TempData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and TempData performs additional responsibility. Lets discuss or get key points on those three objects: Similarities between ViewBag & ViewData :
Difference between ViewBag & ViewData:
ViewBag & ViewData Example:
In View:
TempData: TempData is also a dictionary derived from TempDataDictionary class and stored in short lives session and it is a string key and object value. The difference is that the life cycle of the object. TempData keep the information for the time of an HTTP Request. This mean only from one page to another. This also work with a 302/303 redirection because it’s in the same HTTP Request. Helps to maintain data when you move from one controller to other controller or from one action to other action. In other words when you redirect, “TempData” helps to maintain data between those redirects. It internally uses session variables. Temp data use during the current and subsequent request only means it is use when you are sure that next request will be redirecting to next view. It requires typecasting for complex data type and check for null values to avoid error. Generally used to store only one time messages like error messages, validation messages.
The last mechanism is the Session which work like the ViewData, like a Dictionary that take a string for key and object for value. This one is stored into the client Cookie and can be used for a much more long time. It also need more verification to never have any confidential information. Regarding ViewData or ViewBag you should use it intelligently for application performance. Because each action goes through the whole life cycle of regular asp.net mvc request. You can use ViewData/ViewBag in your child action but be careful that you are not using it to populate the unrelated data which can pollute your controller. |
|||
add comment |
2
|
TempDataBasically it's like a DataReader, once read, data will be lost. Check this VideoExample
If you pay attention to the above code, RedirectToAction has no impact over the TempData until TempData is read. So, once TempData is read, values will be lost. How can i keep the TempData after reading?Check the output in Action Method Test 1 and Test 2
If you pay attention to the above code, data is not lost after RedirectToAction as well as after Reading the Data and the reason is, We are using In this way you can make it persist as long as you wish in other controllers also.ViewBag/ViewDataThe Data will persist to the corresponding View |
|||
add comment |
0
|
ViewBag, ViewData, TempData and View State in MVC http://royalarun.blogspot.in/2013/08/viewbag-viewdata-tempdata-and-view.html ASP.NET MVC offers us three options ViewData, VieBag and TempData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and TempData performs additional responsibility. Similarities between ViewBag & ViewData :
Difference between ViewBag & ViewData:
ViewBag & ViewData Example:
In View, we call like below:
TempData:
The only scenario where using TempData will reliably work is when you are redirecting. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view. It requires typecasting for complex data type and check for null values to avoid error.
|
|||
add comment |