Viewdata TempData

Source: Internet
Author: User

ASP. NET MVC provides 3 choices of viewdata,viewbag,tempdata to transfer data from controller to view and subsequent requests.

ViewData and ViewBag are very similar, while tempdata are different.

Let's discuss or summarize the main points of the three:

The difference between ViewBag and ViewData:

    1. ViewData is a Dictionary object that inherits from the Viewdatadictionary class and has a string (string) that accesses the object as a key.
    2. ViewBag is a dynamic property and is a good dynamic feature added by c#4.0.
    3. ViewData requires a data type conversion to determine whether the book is null to prevent errors.

4.ViewBag does not require a type conversion.

5.ViewBag and Viedata can only be used in one action method, two Aciton with the same name is still the meaning of 2, not a Aciton method

Examples of ViewBag & ViewData:

  1. public ActionResult Index()
  2. {
  3. ViewBag.Name = "Monjurul Habib";
  4. return View();
  5. }
  6. public ActionResult Index()
  7. {
  8. ViewData["Name"] = "Monjurul Habib";
  9. return View();
  10. }

  1. @ViewBag.Name
  2. @ViewData["Name"]


This seems to be a different action function to use, if not in the same action function!

TempData is also a dictionary type, inherited from the Tempdatadictionary class. stored in a short session. The key is of type string and the value is type object.

The difference is the life cycle of the object. The TempData life cycle is the time of an HTTP request. It means the time to jump from one page to another. Keep 302/303 redirected because they are in the same request.

  1. public ActionResult Page()
  2. {
  3. TempData["v"] = "jingya";
  4. return View();
  5. }
  6. public ActionResult Rank()
  7. {
  8. return Content(TempData["v"].ToString());
  9. }

Save your data when you jump from one control to another control or one page to another page. It uses the session mechanism internally. It is used primarily to store data during jumps from one page to another.

Data type conversions are required to check for NULL to circumvent errors. Typically used to store a time information, error message, or authentication information.

  1. public ActionResult Index()
  2. {
  3. = TempData["name"];
  4. return View();
  5. }
  6. public ActionResult About()
  7. {
  8. ViewBag.Message = "Your application description page.";
  9. TempData["name"] = "jingya";
  10. return RedirectToAction("Index");
  11. }

The last mechanism is the session, working like ViewData, dictionary type, such as key-value mode, the key is generally string type, the value can be the object type. In this way, the data is stored in the client's cookie and can be kept for a longer period of time. Row validation is also required, but there is no security guarantee. About ViewData and viewbag you need a more flexible should be shipped in the program. Because each page moves through the entire life cycle of the ASP. You can use Viewdata/viewbag in a sub-page, but be careful that you are not using it to populate irrelevant data and contaminate your controller.  

From for notes (Wiz) Viewdata TempData

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.