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'
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?