I am using MVC (for the first time) with Entity infrastructure, database first
What I want to do is display the data from the database in one view. First I created a database, then I created an Entity Data ADO.NET data model based on a database containing all the tables. Then I created an index view that was strongly typed with my entity data model as a model.
In my index at the top
@model IEnumerable<Forum6.Models.Forum>
This allows me to retrieve rows from the Forum table from my database. If I try to add an additional model, I get an error message at startup:
Line 1: @model IEnumerable<Forum6.Models.Forum> Line 2: @model2 IEnumerable<Forum6.Models.Post>
Parser error message: only one model statement is allowed in a file.
After searching for the answer, I found the following: Two models in one view in ASP MVC 3 The answer was to create a ViewModel (ParentModel) that contained all the Models (Tables). This is the ViewModel I created:
public class ViewModel { public IEnumerable<Forum6.Models.Forum> Forum { get; set; } public IEnumerable<Forum6.Models.Post> Post { get; set; } public IEnumerable<Forum6.Models.Topics> Topics { get; set; } public IEnumerable<Forum6.Models.Users> Users { get; set; } public IEnumerable<Forum6.Models.PrivMsg> PrivMsg { get; set; } public IEnumerable<Forum6.Models.Permission> Permission { get; set; } }
I edited my controller like this:
public class HomeController : Controller {
Then it replaced the old index with a new strongly typed view that used the ViewModel as a model. What contains:
@model IEnumerable<Forum6.Models.ViewModel>
Trying to run this gives me this error:
The model element passed to the dictionary is of the type "Forum6.Models.ViewModel", but this dictionary requires a model element of the type "System.Collections.Generic.IEnumerable`1 [Forum6.Models.ViewModel]
How to make "ViewModel" enumerable? Or is my mistake elsewhere?