We used Session, ViewState, Querystring, Hiddenfield variables in .Net for persisting value. In MVC we can maintain user session in three ways tempdata, viewbag, viewdata.
Here is the difference between them and in which case we need to use that?
TempData :
- tempdata value maintain from One controller to other controller or from one action to another action. tempdata works internally same like session.
tempdata is available for current request, it will not available for subsequest request after read or assign to variable. if we want to use for
subsequest request as well then we need to use "Keep" Method which is used as below code.
string UserName = tempdata["Name"];
tempdata.keep("Name");
- Controller to Controller - in this case, we need to use Tempdata.
View data:
- viewdata value maintain when you move from Controller to View.
- Controller to View - in this case, we need to use Viewdata.
View Bag:
- viewbag is used dynamic object internally. type casting is not required at the time of assing value.
- Controller to View - in this case, we need to use Viewbag.
I hope you like this post. If you have any query then post your comment here or you can contact me at mehulpatelk152@gmail.com.
No comments:
Post a Comment