The ASP.NET MVC is a web application framework developed by Microsoft. ASP.NET MVC is also one of the programming models supported by Microsoft ASP.NET technology. It is an open-source framework while ASP.NET Web Forms was a complete proprietary of Microsoft.
ASP.NET MVC is a framework developed by Microsoft to build web applications. ASP.NET MVC is built on the top of ASP.NET. It used the ASP.NET pipeline for request processing.
Though, MVC is based on Model-View-Controller design pattern.
Please find below the explanation of Model, View & Controller:
Advantages of ASP.NET MVC over Web Forms:
ViewData, ViewBag & TempData all these are state management techniques in ASP.NET MVC.
ViewData & ViewBag are quite similar. Both are used to transfer information from the controller's action to view. But below are some key differences between them.
Syntax for ViewData:
ViewData["Email"] = "ankushjain358@gmail.com";
Syntax for ViewBag:
ViewBag.Email = "ankushjain358@gmail.com";
TempData is a bit different, this is used to transfer information from one controller action to another controller action during full page redirects. TempData uses session behind the scenes.
Syntax for TempData:
TempData["Email"] = "ankushjain358@gmail.com";
ViewData & ViewBag both are the properties of
ControllerBase
class. Type of ViewData isViewDataDictionary
while type of ViewBag isdynamic
. Visit ControllerBase class here.
TempData stores the data in TempDataDictionary
. So, whenever you read any object from the TempDataDictionary
, that is marked for the deletion. And on the subsequent requests, you won't be able to access those objects.
Keep() & Peek() are two methods that allow you to prevent objects from getting deleted after they read once.
Generally, you read a value from the TempData like below & that TempData deletes the object (both key & value) once they are read.
object name = TempData["Name"]
Now, you if read this value from Peek()
method, then this object will not be marked for deletion. It means that you can prevent an object in TempData from being deleted by reading it's value using Peek()
method.
Keep()
is also used to prevent an object in TempData from being deleted but in this, we explicitly call Keep()
method after reading the object from TempData.
//"Name" is marked for deletion
object name = TempData["Name"];
//later on we decide to keep it
TempData.Keep("Name");
We should use
Peek()
when we always want to retain the value. And should useKeep()
when we want to retain object's value on some condition basis.
One more difference, Peek()
returns value while Keep()
doesn't return anything.