MVC - several models in view

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 { // ForumDBEntities old_db = new ForumDBEntities(); ViewModel db = new ViewModel(); public ActionResult Index() { return View(db); } } 

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?

+4
source share
2 answers

You will need to change @model IEnumerable<Forum6.Models.ViewModel> to @model Forum6.Models.ViewModel , since you are @model Forum6.Models.ViewModel your IEnumerables inside the same ViewModel.

A good rule of thumb is to have a 1: 1 relationship between the ViewModel and the View. enter image description here

It may be useful to read for you: http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/ (just ignore the automapper part if you don't want to go this route )

You will also need to put the actual data in the ViewModel, since

 ViewModel db = new ViewModel(); public ActionResult Index() { return View(db); } 

just give your view an empty ViewModel. One way to do this would be.

 public ActionResult Index() { var model = new ViewModel { Forum = db.GetForum(), Post = db.GetPost(), Topic = you get the idea }; return View(model); } 

Last, when the names of properties or variables in the general case, you should use the plural verb when it contains a list. So your ViewModel will be.

 public class ViewModel { public IEnumerable<Forum6.Models.Forum> Forums { get; set; } public IEnumerable<Forum6.Models.Post> Posts { get; set; } public IEnumerable<Forum6.Models.Topics> Topics { get; set; } public IEnumerable<Forum6.Models.Users> Users { get; set; } public IEnumerable<Forum6.Models.PrivMsg> PrivMsgs { get; set; } public IEnumerable<Forum6.Models.Permission> Permissions { get; set; } } 
+10
source

Change @model IEnumerable<Forum6.Models.ViewModel> to @model Forum6.Models.ViewModel , because you are passing one instance of the ViewModel class, not a set of them.

All your collections are transferred in one instance of the presentation model.

+1
source

All Articles