Mvc why use viewmodel




















Then in such situations like this, we need to use the concept ViewModel. NET MVC application is a model which contains more than one model data required for a particular view. Let us have a look at the following diagram which shows the visual representation of a ViewModel in the MVC application. Let say we want to display the employee details on a webpage. And in our application, we have two different models to represent the employee data.

The Employee Model is used to represent the basic details of an employee whereas the Employee Address model is used to represent the employee address. Along with the above two models to represent the employee data, we also required some static information like page header and title in the view.

In order to achieve this, here we need to create a view model such as EmployeeDetailsViewModel. In ASP. This conversion into single object provides us better optimization. You can see the concept of ViewModel in the image below. As you can see, if we want to display more than one Model into a single View, we have to pass a ViewModel to that View, so that we can take benefits of both the models into a single object. So, we have to use ViewModel for better performance of sources.

The View needs to only render the single ViewModel object, and there is a specific purpose for each and every aspect of the application, which means the application will be more organized in the code. Using ViewModel, we put our manipulation code separately in a specific place and away from the View and Controller. So, it provides us a better SoC and makes our code more maintainable. It also makes our code more testable because unit testing means testing the small units, and it is easier to test if we separate the manipulation logic in a separate ViewModel.

These are the complex scenarios in which ViewModel can be used for better maintainability, reliability, and testability of code. Data that lives in small lists and infrequently changes, like the StatesDictionary class, exists in all types of applications. You can replace the code in the StateDictionary method to use entities from Entity Framework, read data from files, or any data access code that you require.

After creating the ViewModel, the next steps are to instantiate it in a controller and return it to the view. Sending a ViewModel to the view for rendering will work the same as when dealing with a model. You can create the instance of the ViewModel class in the controller, or resolve it if using an IoC container. Remember that just as you would do with views, you should keep controllers clean of unnecessary code, meaning that only code that fetches the model or ViewModel belongs here, and little more.

In order for the view to know what object to use, set the model keyword to point to the ViewModel, just like you already would with a regular model.

Property syntax to access the ViewModel data, similar to the following line of code. ViewModels, however, provide those extra benefits like being easier to test and optimize. You can manually map small ViewModels, but this will quickly become burdensome when mapping larger classes, especially when working with parent-child-grandchild, multi-level, or complex data. This is where a tool such as AutoMapper comes into play. AutoMapper will let you fluently setup mappings between ViewModels and models more easily than doing so manually, or writing your own mapper.

POCO Generator. In addition to these tools, you can use MvcScaffolding to create actions and views based on ViewModels. MvcScaffolding, invention of ASP. MvcScaffolding works with ViewModels as well as models. This is the general naming convention followed. Though it is not compulsory to name your files and methods in this manner, it is a part of good programming practice that should be followed. Create a file named PlatformController. Models; using System. Now, we will get the values of ViewModel and display it according to our choice in the order and structure we want in the view part of the code.

For this, create a file named PlatformTutorials. Here we discuss to implementing two models, how to create view file, ViewModel and controller file.

You can also go through our other related articles to learn more —. Submit Next Question. By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password?



0コメント

  • 1000 / 1000