ViewBag, ViewData, TempData and Session are the options to send value from controller to view or to other action method/pages. ViewData and ViewBag are very straight forward, they used to send the data from controller to view and done but in case of TempData and Session we can pass values from controller to view and even we can get our value from other pages after redirecting. As we know TempData and Session they both are saved into session but their behavior is a little different, in this article we will explore ViewBag and ViewData a little bit and more on TempData and Session with different examples.
Let's take a very simple example of ViewData to pass the data from controller to view and show in view
// In controller
ViewData["item"] = "Some string";
Or
ViewData("item", "Some string");
// In view
<div>@ViewData["item"]</div>
It will render the string on the page, very simple, Can we pass object into ViewData, yes we can, let's try it as well,
create a simple class to test it.
public class Product
{
public int Id { get; set; }
public string Product { get; set; }
}
// In controller add to view data
ViewData["product"] = new Product { Id = 1, Product = "Computer" };
// In View
@{
var vdProduct = ViewData["product"] as SessionLess.Controllers.Product;
<div>
View Data Values
<div>
@vdProduct.Id : @vdProduct.product
</div>
</div>
}
//And here is the output:
View Data Values
1 : Computer
ViewData Conclusion:
public ViewDataDictionary ViewData { get; set; }
ViewBag behaves in very similar to ViewData so no need to take examples but just give the Conclusion
public dynamic ViewBag { get; }
TempData
Now time to examine TempData, lets first note it’s feature before applying in code
public TempDataDictionary TempData { get; set; }
Let's try to examine it:
// Assign value in action View1
TempData["item"] = "TempData value to show";
// To render in view
TempData : @TempData["item"]
Let's try to render the value from two different views, say view1 and view 3
Other scenario:
It can be handy in some situation like you want to assign once and read only once, say
As we know now, once temp data is read it will no longer be available, so If we want to keep it after reading it once then we can use Keep
pr Peek
@TempData[“MyData”];
TempData.Keep(“MyData”);
First read and then indicate to keep it for further use, but if you want to do the same thing in one line then use Peek like this
var str = TempData.Peek("MyData").ToString();
In this way you can use temp data as many times as you want or you can say in this way it will work like a session.
Session
![]() |
Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
|
By Ali Adravi On 15 Nov, 14 Viewed: 6,024 |
MVC Searh page with pagination: It’s very easy to create a search page in asp.net but when I try to create the same in MVC I faced many problems, how to create model, how to keep searched values in search controls, pagination, I found myself nowhere, so start searching for some good examples but... By Ali Adravi On 25 Aug 2013 Viewed: 40,268
CRUD (Create, Retrieve, Update and Delete) in MVC. When we start to learn new language, first we try to run an application with “Hello World” and then CRUD functionality. So in this article we will see how to select records from database (with WebGrid, pagination and sort functionality), update a... By Ali Adravi On 17 Aug 2013 Viewed: 106,142
Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or... By Hamden On 30 Jun 2013 Viewed: 3,793
In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the... By Ali Adravi On 29 Jun 2013 Viewed: 7,049
Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi On 04 Jun 2013 Viewed: 25,605