3 August 2013

ViewBag, ViewData, TempData and View State in MVC

ViewBag, ViewData, TempData and View State in MVC

ASP.NET MVC offers us three options ViewData, ViewBag and TempData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and TempData performs additional responsibility. 


Similarities between ViewBag & ViewData :
  1. Helps to maintain data when you move from controller to view.
  2. Used to pass data from controller to corresponding view.
  3. Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.
Difference between ViewBag & ViewData:
  1. ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  3. ViewData requires typecasting for complex data type and check for null values to avoid error.
  4. ViewBag doesn’t require typecasting for complex data type.
ViewBag & ViewData Example:


public ActionResult Index()

{

    ViewBag.Name = "Arun Prakash";
    return View();
}

public ActionResult Index()
{
    ViewData["Name"] = "Arun Prakash";
    return View();

View
@ViewBag.Name 
@ViewData["Name"]

TempData:
Helps to maintain data when you move from one controller to other controller or from one action to other action. In other words when you redirect, “Tempdata” helps to maintain data between those redirects. It internally uses session variables. TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only

The only scenario where using TempData will reliably work is when you are redirecting. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view.

It requires typecasting for complex data type and check for null values to avoid error.

public ActionResult Index()
{
  var model = new Review()
            {
                Body = "Start",
                Rating=5
            };
    TempData["ModelName"] = model;
    return RedirectToAction("About");
}
public ActionResult About()
{
    var model= TempData["ModelName"];
    return View(model);
}

View State in MVC
ASP.NET MVC does not have ViewState (that of storing the values of controls in the web page).
So ASP.NET MVC pages are incredibly lightweight and your request and response times are much faster.

Alternative to View State in MVC
1. For retaining the values during postback in the MVC page values you can use Ajax, so that values in the     control won't clear.
2. For temporarily storing the value in current and the subsequent requests use TempData.








35 comments:

  1. Thanks. nice, its usefull forme

    ReplyDelete
  2. Very good article.

    Thanks A Lot.

    ReplyDelete
  3. Very Nice One

    ReplyDelete
  4. Need More Information

    ReplyDelete
  5. Hi Thank For Visit us:

    ARKA Softwares & Outsourcing is an IT Company focusing on software & Web development and providing offshore outsourcing solutions to enterprises worldwide.website designing company in australia

    ReplyDelete
  6. It's A good answer.
    Thank For Visit my blog:

    http://sibinkt.blogspot.in/

    ReplyDelete
  7. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training institute in velachery | top10 dot net training institutes in chennai

    ReplyDelete
  8. nice it is really helpfull

    ReplyDelete
  9. This blog gives very important info about .Net Thanks for sharing
    .Net Online Training

    ReplyDelete
  10. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.


    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  11. I really like your blog. You make it interesting to read and entertaining at the same time. I cant wait to read more from you.
    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  12. Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!
    python training in chennai
    python training in Bangalore

    ReplyDelete
  13. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai
    Devops Training in Bangalore

    ReplyDelete
  14. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  15. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    selenium training in chennai
    selenium training in bangalore
    selenium training in Pune
    selenium training in pune
    Selenium Online Training


    http://seleniumready.blogspot.com/2011/01/program-to-login-into-webpage-using.html

    ReplyDelete
  16. I glad to read this post thanks for sharing
    IOT training in chennai

    ReplyDelete
  17. Hey! Fabulous post. It is the best thing that I have read on the internet today. Moreover, if you need instant support for QuickBooks Error, visit at QuickBooks Customer Service Our team is always ready to help and support their clients.

    ReplyDelete
  18. Link:Title:
    Best Big Data Hadoop Training in Chennai | Infycle Technologies

    Description:
    Dream your career towards Big Data? Then come to Infycle Technologies, the best software training center in Chennai, which gives the combined and best Big Data Hadoop Training in Chennai, in a 100% hands-on training guided by professional teachers in the field. In addition to this, the interviews for the placement will be guided to the candidates, so that, they can face the interviews without struggles. Apart from all, the candidates will be placed in the top MNC's with a bet salary package. Call 7502633633 and make this happen for your happy life.

    best training institute in chennai

    ReplyDelete
  19. Hi , Thank you so much for writing such an informational blog. If you are Searching for latest Jackets, Coats and Vests, for more info click on given link-Rip Wheeler Jacket

    ReplyDelete
  20. Infycle Technologies, the No.1 software training institute in Chennai offers the best Oracle training in Chennai for students, freshers, and tech professionals. In addition to Oracle, other in-demand courses such as Data Science, Big Data, Java, AWS, Machine Learning, Python, Power BI, Digital Marketing will be trained with 200% practical classes. Once the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7502633633 to get more info and a free demo.Increase Domain Authority (DA)

    ReplyDelete
  21. excellent blog thanks for information
    Deepam Oil

    ReplyDelete
  22. Nice Blog !
    Here We are Specialist in Manufacturing of Movies, Gaming, Casual, Faux Leather Jackets, Coats And Vests See. Phoenix Valorant Jacket

    ReplyDelete
  23. Your site is good Actually, i have seen your post and That was very informative and very entertaining for me. bella swan jacket

    ReplyDelete
  24. Thanks for this. This is the simplest explanation I can understand given the tons of Explanation here. bella swan jacket

    ReplyDelete
  25. Our Data Science certification training with a unique curriculum and methodology helps you to get placed in top-notch companies. Avail all the benefits and become a champion.
    data scientist certification malaysia

    ReplyDelete

Comments Welcome

Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...