Saturday, November 19, 2011

ViewBag, ViewData, or TempData in ASP.NET MVC 3

 

 

In Mvc This 3 object work like property and use in view and controller.  basic use is get and pass value means communication between  view controller. using this you can get or set chuck amount of data.

like here is Example

// Comment
viewdata["Name"]="Bhavik Solucky";
viewbag.Name="Bhavik Solucky"; 
tempdata["Name"]="Bhavik Solucky";

 


now here I have one view which display this value


// Comment
Temp: @TempData["Name"]
ViewData: @ViewData["["Name"];"]
viewbag:viewbag.Name;
now when we come this view all 3 variable display value

but basic difference of all 3 variable is that if you want to redirect from one view to other view then tempdata will contain value and other view doesn’t contain any detail.


suppose redirect from home view to customer view then tempdata contain value but viewbag and view data become null


more detail check this link :http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

so when you know redirect request that time temp data is very useful

No comments:

Post a Comment