Search code examples
c#asp.netasp.net-mvcrazor

ASP.NET MVC error: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1


Whenever I show this view Index.cshtml:

@model IEnumerable<Web_Course.Models.Movie>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Movies</h2>

<table class="table table-bordered table-hover">
    <thead>
    <tr>
        <th>Movie</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var movie in Model)
    {
        <tr>
            <td>@movie.Name</td>
        </tr>
    }
    </tbody>
</table>

I get this error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Web_Course.Models.Movie]', but this dictionary requires a model item of type 'Web_Course.ViewModels.RandomMovieViewModel'

Screenshot

I made sure to pass IEnumerable<Movie> to the View() and the model in the view file.

Here is the controller:

public class MovieController : Controller
{
    // GET: Movie
    public ActionResult Index()
    {
        var movies = GetMovies();
        return View(movies);
    }

    private IEnumerable<Movie> GetMovies()
    {
        return new List<Movie>
        {
            new Movie { Id = 1, Name = "John Wick" },
            new Movie { Id = 2, Name = "Oppenheimer" }
        };
    }
}

Here is the Movie class:

namespace Web_Course.Models
{
    public class Movie
    {
        public int Id { get; set; }
        public string Name { get; set; }
        

    }
}

And here is the view model that is shown in the error:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web_Course.Models;

namespace Web_Course.ViewModels
{
    public class RandomMovieViewModel
    {
        public Movie Movie { get; set; }
        public List<Customer> Customers { get; set; }
    }
}

I don't know how this file is related to the error.

What is the issue here?


Solution

  • It is necessary to fix the data model declaration in the Index.cshtml view to:

    @model IEnumerable<Web_Course.Models.Movie>
    

    You didn't show declaration of the namespace where the Movie item is declared.

    In case after this update you still have an issue fix your question to show name space where the Movie class is declared.

    UPDATE:

    The problem in your _Layout.cshtml:

    @using Web_Course.Models
    @model Web_Course.ViewModels.RandomMovieViewModel
    <!DOCTYPE html>
    <html>
    ...
    

    By this line Layout = "~/Views/Shared/_Layout.cshtml"; in your Index.cshtml you instruct the MVC engine to use this layout _Layout.cshtml when rendering the Index.cshtml. The view data model in this template is declared as Web_Course.ViewModels.RandomMovieViewModel type. But the current model in the Index.cshtml is IEnumerable<Web_Course.Models.Movie>. This is the reason why you got this error.

    You can comment the following line in the _Layout.cshtml and the Index() action method will render the view without a problem:

    @*@model Web_Course.ViewModels.RandomMovieViewModel*@
    

    You can found lot of examples on the StackOverflow how to pass a data model to layouts. For example, see: ASP.NET MVC Razor pass model to layout