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 :
- Helps to maintain data when you move from controller to view.
- Used to pass data from controller to corresponding view.
- 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:
- ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
- ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- ViewData requires typecasting for complex data type and check for null values to avoid error.
- 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.
Thanks. nice, its usefull forme
ReplyDeleteVery good article.
ReplyDeleteThanks A Lot.
Very Nice One
ReplyDeleteNeed More Information
ReplyDeleteHi Thank For Visit us:
ReplyDeleteARKA Softwares & Outsourcing is an IT Company focusing on software & Web development and providing offshore outsourcing solutions to enterprises worldwide.website designing company in australia
It's A good answer.
ReplyDeleteThank For Visit my blog:
http://sibinkt.blogspot.in/
It is usefull
ReplyDeleteIt's good information
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
ReplyDeleteThanks 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
nice it is really helpfull
ReplyDeleteThis blog gives very important info about .Net Thanks for sharing
ReplyDelete.Net Online Training
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.
ReplyDeleterpa 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
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..
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
iot-training-in-chennai
blueprism-training-in-pune
automation-anywhere-training-in-pune
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeleterpa Training in annanagar
blue prism Training in annanagar
automation anywhere Training in annanagar
iot Training in annanagar
rpa Training in marathahalli
blue prism Training in marathahalli
automation anywhere Training in marathahalli
blue prism training in jayanagar
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.
ReplyDeletejava training in tambaram | java training in velachery
java training in omr | oracle training in chennai
java training in annanagar | java training in chennai
Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!
ReplyDeletepython training in chennai
python training in Bangalore
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..
ReplyDeleteDevOps online Training|DevOps Training in USA
Devops Training in Chennai
Devops Training in Bangalore
Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
ReplyDeleteBlueprism training in btm
Blueprism online training
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick 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
I glad to read this post thanks for sharing
ReplyDeleteIOT training in chennai
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.
ReplyDeleteLink:Title:
ReplyDeleteBest 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
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
ReplyDeleteInfycle 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)
ReplyDeletegreat blog thanks for information
ReplyDeleteSphatik mala
Mahameru Panchaloha
excellent blog thanks for information
ReplyDeleteDeepam Oil
excellent blog thanks for information
ReplyDeletepooja store near me
sivan pattai
tamil monthly calendar
panchaloha idols
geat blog thank you
ReplyDeletedeepam Oil Wholesale
Nice Blog !
ReplyDeleteHere We are Specialist in Manufacturing of Movies, Gaming, Casual, Faux Leather Jackets, Coats And Vests See. Phoenix Valorant Jacket
Your site is good Actually, i have seen your post and That was very informative and very entertaining for me. bella swan jacket
ReplyDeleteThanks for this. This is the simplest explanation I can understand given the tons of Explanation here. bella swan jacket
ReplyDeleteOur 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.
ReplyDeletedata scientist certification malaysia
Tramadol UK
ReplyDelete