ASP.NET MVC

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.

  • Model: Model denotes DTOs or POCO Classes or View Models that will be used to bind data to view.
  • View: View here represents the UI layer. All the HTML required to render a page is written in this section.
  • Controller: Controller is the main part. It captures the control when a request arrives, Action method defined in the controller gets called. This action method fills data in ViewModels & then we return a view that will render that data on some HTML page.

Please find below the explanation of Model, View & Controller:

  • Model: Model denotes DTOs or POCO Classes or View Models that will be used to bind data to view.
  • View: View here represents the UI layer. All the HTML required to render a page is written in this section.
  • Controller: Controller is the main part. It captures the control when a request arrives, Action method defined in the controller gets called. This action method fills data in ViewModels & then we return a view that will render that data on some HTML page.

Advantages of ASP.NET MVC over Web Forms:

  1. More control over HTML markup. In ASP.NET Web Forms, we remain dependent on the markup generated by ASP.NET Controls.
  2. Request pipeline is quite easy & straight-forward. As MVC does not have any page lifecycle events.
  3. Better Routing Support.
  4. No ViewState concept.
  5. CSS & JS Bundling support.
  6. SEO Friendly URLs
  7. Enables Test Driven Development.

ViewData, ViewBag & TempData all these are state management techniques in ASP.NET MVC.

ViewData and ViewBag

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.

  1. ViewData uses the dictionary to store the data while ViewBag is a dynamic object.
  2. Type Casting is required to retrieve the data from ViewData while in case of ViewBag, there is no need of TypeCasting.
  3. ViewData is faster as it uses Dictionary in the background.

Syntax for ViewData:

ViewData["Email"] = "ankushjain358@gmail.com";

Syntax for ViewBag:

ViewBag.Email = "ankushjain358@gmail.com";

TempData

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 is ViewDataDictionary while type of ViewBag is dynamic. 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.

Peek() in TempData

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() in TempData

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 use Keep() when we want to retain object's value on some condition basis.

One more difference, Peek() returns value while Keep() doesn't return anything.

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. For more information - please visit our private policy.