MVC - Multiple models in a view

asp.net-mvc entity-framework

Question

I'm using MVC with Entity Framework and Database First for the first time.

I want to use a single view to present data from a database. I constructed an ADO.NET Entity Data Model based on the database that contains all the tables after initially creating the database. Then, using my Entity Data Model as the model, I constructed an Index view that was highly typed.

I have at the top of my Index

@model IEnumerable<Forum6.Models.Forum>

This enables me to retrieve the entries from my database's "Forum" table. I get the following problem message when I try to run an additional model:

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.

I discovered this after looking for an answer: ASP MVC 3 uses two models in a single view. Making a ViewModel (ParentModel) that included all the Models was the solution (Tables). Here is the viewmodel that I developed:

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 altered my controller to read as follows:

   public class HomeController : Controller
{
    // ForumDBEntities old_db = new ForumDBEntities();

    ViewModel db = new ViewModel();

    public ActionResult Index()
    {
        return View(db);
    }
}

Then, a new strongly typed view using the ViewModel as its model was utilized to replace the previous Index view. which includes:

@model IEnumerable<Forum6.Models.ViewModel>

When I try to execute it, I get the following error:

The model item passed into the dictionary is of type 'Forum6.Models.ViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Forum6.Models.ViewModel]

How do I enumerate the "ViewModel"? Or is my mistake somewhere else?

1
4
5/23/2017 12:18:22 PM

Accepted Answer

You'll have to alter.@model IEnumerable<Forum6.Models.ViewModel> to @model Forum6.Models.ViewModel as your IEnumerables are contained within a single ViewModel.

Having a 1:1 link between your ViewModel and View is a solid general rule of thumb.enter image description here

If you don't want to go that route, just disregard the automapper part of this article: http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/.

Additionally, you must include real data in your ViewModel because

ViewModel db = new ViewModel();

public ActionResult Index()
{
    return View(db);
}

will just provide your view with a blank ViewModel. One approach would be.

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

    return View(model);
}

One more thing: whenever a verb involves a list when naming properties or variables in general, use the plural form. So would your ViewModel.

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
3/12/2013 1:52:15 PM

Popular Answer

Change @model IEnumerable<Forum6.Models.ViewModel> to @model Forum6.Models.ViewModel considering that you are passing a single instance of aViewModel class rather than a group of them.

A view model instance receives all of your collections at once.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow