You must Sign In to post a response.
  • Category: ASP.NET

    How to ViewBag, ViewData and Tempdata in MVC

    my question is how and when to use viewdata, viewbag and Tempdata
  • #763633
    you can use both for many purpose following is a tutorial for letting you know how and when to use them.


    Delete Attachment

  • #763634
    for temp data check this one man


    Delete Attachment

  • #763640

    Before asking this question you should read few articles either in Online or books, refer one of the best link for the same and after reading this still you have questions let us know.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #763642
    Hai Fakhr Shahab,
    Below are the differences between them:
    View Bag:
    1. Its a dynamic property which uses the dynamic keyword internally. So we can assign any kind of data even during the run time.
    2. It is uses the pass data from controller to view.
    3. The data will not be available after the view is read it so the data cant be transferred for next request.
    4. As its dynamic, so doesn't require typecasting
    View Data:
    1. Its dictionary object so data is kept in key value pair.
    2. Typecasting is needs when retrieving the data.
    3. Also can be passed the data from Controller to view and becomes null for the next request.
    Temp Data:
    1. Also a dictionary based object where the data is kept in key-value pair.
    2. Data needs to be typecast ed when used.
    3. Data can be used from controller to view and then view to the controller.
    4. Data is available in the next request also.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #763654
    Hi Fakhr Shahab

    ViewBag and ViewData are used to send the information from controller to the respected view for one time.
    We can say the scope of these viewbag and viewdata are limited i.e., from controller - view.
    If you want to get some information back from view to controller then we use tempdata variable (like we use session variable in
    When we are redirecting from one page to another we use tempdata.

    TempData["variable"] = "variable1";
    ViewBag.variable= "variable1";

    Sridhar Thota.
    Editor: DNS Forum.

Sign In to post your comments