Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - Multiple models in a view

I'm using MVC (for the first time) with Entity framework, Database first

What I want to do is display data from a database in a single view. I created the database first, then I made a ADO.NET Entity Data Model based from the database that contains all the tables. I then created a Index view that was strongly typed with my Entity Data Model as model.

In my Index I have at the top

@model IEnumerable<Forum6.Models.Forum>

This allows me to get the rows from the table "Forum" from my database. If I try to add an extra model I get I get this error message when I run:

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 an answer I found this: 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 to look like this:

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

    ViewModel db = new ViewModel();

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

Then replaced the old Index view with a new strongly typed view that used the ViewModel as model. Which contains:

@model IEnumerable<Forum6.Models.ViewModel>

Trying to run this gives me this 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 make the "ViewModel" enumarable? Or is my error elsewhere?

like image 205
NoClueBlue Avatar asked Dec 20 '22 08:12

NoClueBlue


1 Answers

You'll need to change @model IEnumerable<Forum6.Models.ViewModel> to @model Forum6.Models.ViewModel as you're wrapping your IEnumerables inside a single ViewModel.

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

This might be a good 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 that route)

You'll also need to put in actual data in your ViewModel since

ViewModel db = new ViewModel();

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

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

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

    return View(model);
}

One last thing when naming properties or variables in general you should use the plural verb when it contains a list. So your ViewModel would 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; }
}
like image 121
Snæbjørn Avatar answered Jan 08 '23 07:01

Snæbjørn