Reputation: 33
I am new to Entity and Visual Studio. I have two models already populated with data and the third one is originally part of the controller that I want to implement the other two models in. I don't want to edit the other two models but just show the data already in the table.
I have tried using tuple using this tutorial Multiple models in a view but am unable to use @foreach statements.
@model Tuple<App.Models.ValueForecast, App.Models.FacilityEditor, App.Models.ForecastSub>
Whats the easiest way of being able to have three models in one view? The other two models must be able to display the data from their table data.
I know there is a method with a viewModels but the data from the table is not showing up, I used this method: Two models in one view in ASP MVC 3 but did not work
Upvotes: 0
Views: 184
Reputation: 1573
Just create an object to transfer the data, you need to distinguish between your domain model (Where the logic of the problem should reside), you data model (Normally the ones that are mapped to databases and stuff), and your Data Transfer Objects (which are the who only reason to live is to encapsulate you objects to be passed between methods).
You need a DTO when you are passing too many parameters to one method, or when you need several objects in your views (talking about c# and web development) DTOs.
Even in the new c# core and his web development part, many people recommend to have a folder called "ViewModels" where you can place one view model per view, allowing to pass complex objects.
Other link to clarify: What are view models?
A view model represents the data that you want to display on your view/page, whether it be used for static text or for input values (like textboxes and dropdown lists) that can be added to the database (or edited). It is something different than your domain model. It is a model for the view.
So, you should create a new class that act as a container of your other objects, in your case something like:
public class NameOfTheviewViewModel {
public App.Models.ValueForecast ValueForecast { get; set; }
public App.Models.FacilityEditor FacilityEditor { get; set; }
public App.Models.ForecastSub ForecastSub { get; set; }
}
And then use this class as the model for your view:
@model NameOfTheviewViewModel
foreach(var item in Model.ValueForecast) //Or whatever your list are
{
// do something
}
Upvotes: 3
Reputation: 1134
Just create a class (view model) to hold the data that your need and map your 3 different domain class's data to it (this could be done manually or with something like AutoMapper). You will then just declare this view model as the model for your view.
Please note that mapping your domain objects (POCOs) straight to your View is not ideal as you often end up mixing presentation specific code in with your data access layer code.
Separate these concerns into a Data layer with all your POCOs and a presentation layer. Then, when you need to display this data, create a ViewModel to represent/transfer this data between your view and your controller. It is a little more code but is well worth it in my opinion.
Upvotes: 0