I'm looking for some opinions on two different approaches to ViewModel definition
I have a Company class
public class Company { public string Name { get; set; } public int CountryID { get; set; } }
For the Create and Edit views I need a list of Countries to populate a DropDownList for CountryID selection. I can see two broad choices for how to structure the ViewModel that are detailed below.
Nested ViewModel
public class CompanyCreateEditViewModel { public Company Company { get; set; } public IEnumerable<Country> Countries{ get; set; } .... }
Flat ViewModel
public class CompanyCreateEditViewModel { public string Name { get; set; } public int CountryID { get; set; } public IEnumerable<Country> Countries{ get; set; } .... }
At present I'm favoring the Nested approach as it saves me from defining fields for a second time, but I want to throw it open to better approaches and comments.
Thanks
A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.
In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization.
ViewModel = Model that is created to serve the view. ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose. View Model is a model class that can hold only those properties that are required for a view.
You can have methods in your ViewModel .
In this article, you will learn the use of ViewModel in ASP.NET MVC. ViewModel is used to encapsulate the multiple entities into single entity. It is basically a combination of data models into single object and rendering by the view.
Ideally, the ViewModel should contain only data and no logic in it. But you can add View Specific logic to ViewModel. Create one ViewModel for each View. i.e there is a one to one relationship between Views and ViewModels. Use ViewModel even for simple scenarios.
The Model in MVC Pattern stands for View Model and Edit Model. Most people use the term View Model for both View Model and Edit Model. The ViewModel is pretty useful when you have a complex UI, where data needs to be pulled up from several domain models.
It is basically a combination of data models into single object and rendering by the view. Sometimes it is required to show the multiple entities data on view which is coming from different data model classes. So, it includes all the data into single one and makes it flexible to show on View in ASP.NET.
I personally prefer the nested approach for presentation because it leads to a more logical design when you use partial views. You might have a CompanyPartialView
used all across the application that knows how to render a Company
, so it makes a lot of sense to expose the Company
as a nested structure.
On the other hand, flat ViewModel classes are the easiest to work with for data entry. You just have a bunch of form fields that all map to individual properties. So my strategy is usually to flatten them for data entry pages and nest them for presentation/report pages.
I prefer nested, for several reasons:
IMHO, HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With