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?

1 Answers1

1

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

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • It didn't work even after the update it shows the same error, I edited the post and the movie class now has the namespace `Web_Course.Models` – Cover Basha Aug 20 '23 at 04:12
  • @CoverBasha: Now your question contains all the related parts of code and I'm sure your are already defined the data model correctly: `@model IEnumerable`. Your code is compiling and running for me without any errors. Try the following: exit from the Visual Studio, remove `bin` and `obj` folders under your project directory, open your solution in the Visual Studio and make `Rebuild` your project. – Jackdaw Aug 20 '23 at 08:32
  • Still the same error, so you think something is wrong with the project? – Cover Basha Aug 21 '23 at 22:26
  • @CoverBasha: If you can load your project, for example, to the github - we will be able to simulate this bug. Currently, sources that you post doesn't meet to the error description. – Jackdaw Aug 22 '23 at 12:18
  • Here: https://github.com/CoverBasha/Web_Course – Cover Basha Aug 22 '23 at 19:44
  • @CoverBasha: See my updated answer. Regards – Jackdaw Aug 22 '23 at 20:19