Dot Net Stuff

Understanding Best Way to Use Multiple Models in ASP.NET MVC


It is depend upon our requirement to use multiple Models in ASP.NET MVC application, in this article, we will discuss how to choose the most suitable way to use multiple models from controller to view in ASP.NET MVC. In my one of the article, we have seen various way to use multiple Models in ASP.NET MVC application. i.e., ViewModel, Patial View, Tuple, ViewData, ViewBag and TempData in the article "Way to Use Multiple Models in a view in ASP.NET MVC". It may be hard for beginners to decide while selecting a way to be used in a particular scenario. In this article, I will discuss how to choose best way to use multiple Models in ASP.NET MVC application.

NOTE: It is a recommendation to read this article "Way to Use Multiple Models in a view in ASP.NET MVC". Please, read it carefully because this article is depends on that. "Way to Use Multiple Models in a view in ASP.NET MVC" article has complete and detail discussion about using multiple way to use multiple Models in ASP.NET MVC.

Ways to Use Multiple Models

We have discussed 6 approaches in the previous article have their own pros and cons. It is depends on your scenario in hand, which way you should use. That’s why before taking any decision, you need to identify the exact requirement then only we should choose one of those approaches by comparing the pros and cons. Following are the ways to use Multiple Models in a View.

  1. ViewModel
  2. PartialView
  3. ViewData
  4. ViewBag
  5. TempData
  6. Tuple

We will discuss Pros and Cons of all the ways given in the above list.

Multiple Models using ViewModel.

ViewModel is a pattern that allow us to have multiple models as a single class. A ViewModel should not have any methods. ViewModel aggregates models or contains their properties exactly as per the need of a View. We need to have the property or collection of properties which we need at View. ViewModel is the standard way to use multiple Models at View or PartialView. ViewModel is the most widely used approach to pass multiple models to View in enterprise applications.

Advantage of using ViewModel

  • ViewModel is useful to achieve loose coupling in ASP.NET MVC application.
  • ViewModel support intellisense, also it allow to check compile time error at View.
  • ViewModel allows us to render multiple model types in a View as a single model.
  • Using ViewModel is very useful in term of security. We can hide Core domain models to users.

Disadvantage of using ViewModel

  • There is no disadvantage for small application but if we are working on a big application, than ViewModel could be added little complexity.

Multiple Models using Partial View

PartialView is like user control in ASP.NET. We can use several Partial View a parent view. It is very useful to share common code to users. This approach is also frequently used in enterprise applications along with ViewModels. It is used where you need to share the same code (Razor and HTML code) in more than one View.

Advantage of using PartialView

  • We have the option of reusability of code (Razor and HTML code) in application.
  • While developing single page application, PartialView could be very useful.
  • Partial View can be called using AJAX without refreshing the whole page.

Disadvantages of using Partial View

  • Although, there is no big disadvantage of Partial View. But, Adding too many Partial View in a single View could be add some complexity.

Multiple Models using ViewData

ViewData is defined as property (type of ViewDataDictionary class) in ControllerBase class. To access the values stored in ViewData typecasting is require to the corresponding datatype. We can access the values in ViewData using a key.

Advantage of using ViewData

  • ViewData is very useful to send data from controller to View with a key.

Disadvantages of using ViewData

  • The values of ViewData persists only during current request. If any redirection occurs than the values of ViewData becomes null.
  • ViewData is one-way only that is from controller to view. We can’t send data from View to Controller using ViewData.
  • No intelligence support and compile-time error checking. That’s why it will become little complex with big enterprise application.

Multiple Models using ViewBag

ViewBag is a dynamic property which comes from ControllerBase class. Technically, ViewBag stored data as like dictionary. It takes advantage of the new dynamic features in C# 4.0, so ViewBag doesn’t require typecasting for data types. The ViewBag is useful, where Model doesn’t fit our requirement. One of the best example of ViewBag is suing ViewBag.Title.

Advantage of using ViewBag

  • After VewModel, It is the best way to send data from Controller to ViewUsing ViewBag.
  • ViewBag doesn’t require type casting for data types. That’s why it is very easy to use.

Disadvantages of using ViewBag

  • The value of ViewBag persists only during current request. The values will become null after redirection occurs.
  • No intelligence support and compile-time error checking.
  • ViewBag can be used only for send data from Controller to View (one way).

Multiple Models using TempData

TempData is similar to ViewData the difference is that TempData can be used to send and receive the data from one controller to another controller and from one action to another action TempData is defined as property in ControllerBase class. It is a type of TempDataDictionary class. Values stored in TempData require typecasting to datatype in View. The values in TempData are accessible using a key. It is similar to ViewData but the difference is that it allow us. TempData uses session variables, so whenever you need to hold some information till subsequent request, we can use TempData. It is very useful to hold messages or validation information of any other small information. We should not pass sensitive data as it uses session to pass value.

Advantages of using TempData

  • The main advantage using TempData is we can pass value from one action to another action or one controller to another controller.

Disadvantages of using TempData

  • We need to check null to avoid error .It also requires type casting for data type.
  • TempData doesn’t support intellisense in Visual Studio.

Multiple Models using Tuple

A tuple is a data structure that has a specific number and sequence of elements. An example of a tuple is a data structure with three elements (known as a 3-tuple or triple) that is used to store an identifier such as a person's name in the first element, a year in the second element, and the person's income for that year in the third element. The .NET Framework directly supports tuples with one to seven elements. In addition, you can create tuples of eight or more elements by nesting tuple objects in the Rest property of a Tuple object. It may be good for small and demo applications. In ASP.NET MVC, we should use Tuple only when you do not want to create ViewModel.

Advantages of using Tuple

  • Tuple allow us to aggregate models without creating new class. It will help to reduce coding effort than ViewModel.

Disadvantages of using tuple

  • Tuples is fixed size maximum limit of 8 items. The Values of Tuple can be accessible using item1, item2…, that’s why it produce little complexity for developer.

Summary: Now by reading this article carefully, I must say that you will be able to choose the best way to use passing multiple Models in view in your ASP.NET MVC application. We learned how to choose the best way to pass multiple models in ASP.NET MVC application. It is recommended to use multiple Models in ASP.NET MVC application.


Keen to hear from you...!

If you have any questions related to what's mentioned in the article or need help with any issue, ask it, I would love to here from you. Please MakeUseOf Contact and i will be more than happy to help.

About the author

Anil Sharma is Chief Editor of dotnet-stuff.com. He's a software professional and loves to work with Microsoft .Net. He's usually writes articles about .Net related technologies and here to shares his experiences, personal notes, Tutorials, Examples, Problems & Solutions, Code Snippets, Reference Manual and Resources with C#, Asp.Net, Linq , Ajax, MVC, Entity Framework, WCF, SQL Server, jQuery, Visual Studio and much more...!!!

blog comments powered by Disqus