1

I am trying to experiment a little with ASP.NET MVC to get a better feel for it, thus far it has been great, but I am getting a really weird error.

I have a model called Movies.cs with the following code:

public class Movie {
    public int Id { get; set; }
    public string Name { get; set; }  
}

And made a folder called ViewModels with the following file:

public class MoviesMoviesViewModel {
    public List<Movie> Movies { get; set; }
}

My controller makes use of these models as:

 public ActionResult MoviesC()
        {
            var movies = new List<Movie>
            {
                new Movie {Name = "First Movie"},
                new Movie {Name = "Sasha Grey vs the Alien Babes of Mars!"},
                new Movie {Name = "This is actually fun!"}
            };
            var movieviewModel = new MoviesMoviesViewModel
            {
                Movies = movies
            };
            return View(movieviewModel);
        }

Just to finally be used inside my view as:

@model  Vidly.ViewModels.MoviesMoviesViewModel
@{
    ViewBag.Title = "Movies";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Movies</h2>


@if (Model.Movies.Count == 0)
{
    <p>There are no movies to see here.</p>
}
else
{
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Movies</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var movie in Model.Movies)
            {
                <tr>
                    <td><a href="#">@movie.Name</a></td>
                </tr>
            }
        </tbody>
    </table>
}

Just to get an error:

The model item passed into the dictionary is of type 'Vidly.ViewModels.MoviesMoviesViewModel', but this dictionary requires a model item of type 'Vidly.ViewModels.RandomMovieViewModel'.

The weird thing about this error is that RandomMovieVieModel.cs is a file that lives inside the same folder in which MoviesMoviesViewModel.cs lives. But they are not connected in any way whatsoever. I am going through a tutorial and experimenting through it, so I apologize if I am not an alpha master of this. I keep looking around the web to find a solution to this issue but I have been unsuccessful. Any pointers would be greatly appreciated.

BTW. Sorry for the horrible naming conventions, this is code made to be thrown away since I am just trying to get a feel for the framework.

sigillum_conf
  • 423
  • 3
  • 22
  • 2
    The code you have shown will absolutely not throw that error. Its due to code you have not shown. Suggest you read [this question/answer](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) to help find the real cause –  Jul 27 '17 at 01:37
  • I had declared the use of the model in the base layout file....how silly. I had been hitting my head against the wall for almost 20 minutes. Simple fix, it is working now, would you kindly add that as an answer so that I can properly select it and close it? – sigillum_conf Jul 27 '17 at 01:43

0 Answers0