0

Allow me to preface this by saying other SO questions did not answer my issue.

I am fairly new to ASP.net and I am following some online tutorials. Now, I am having a problem passing in a model. The error is:

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

This makes no sense to me, however, as I do not recall setting any dictionary at all. The following code works just fine for my Movies page.

ViewModel:

namespace Vidly.ViewModels
{
    public class RandomMovieViewModel
    {

        public Movie Movie { get; set; }
        public List<Customer> Customers { get; set; }

    }
}

Controller:

namespace Vidly.Controllers
{
    public class MoviesController : Controller
    {
        // GET: Movies/random
        public ActionResult Random()
        {

            var movie = new Movie() { Name = "Shrek!" };

            var customers = new List<Customer>
            {
                new Customer { Name = "Michael"},
                new Customer { Name = "SlowPC"}
            };

            var movieModel = new RandomMovieViewModel
            {
                Movie = movie,
                Customers = customers
            };

            return View(movieModel);
            //return Content("Hello World");
            //return HttpNotFound();

            //return new EmptyResult();
            //return RedirectToAction("Index", "Home", new { page = 1, sortBy = "name" });
        }
}

And in our CSHTML we call it as follows:

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

@{ 
    var className = Model.Customers.Count > 5 ? "popular" : null;
}

Yet, when I do the same thing for my DetailsCustomer it is throwing errors and I am not sure why

ViewModel:

namespace Vidly.ViewModels
{
    public class DetailCustomersViewModel
    {

        public List<Customer> Customers { get; set; }

    }
}

Controller:

namespace Vidly.Controllers
{
    public class CustomersController : Controller
    {

        [Route("customers/details/{Id}")]
        public ActionResult Details(int? Id)
        {

            var customers = new List<Customer>
            {
                new Customer { Id = 1, Name = "Jane Doe"},
                new Customer { Id = 2, Name = "John Smith"}
            };

            var customersModel = new DetailCustomersViewModel
            {
                Customers = customers
            };

            return View(customersModel);
        }
}

cshtml:

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

@{
    var className = Model.Customers.Count > 5 ? "popular" : null;
}


<h2 class="@className">Customers</h2>

As you can imagine, I am totally mystified as to HOW there is a type mismatch when I am literally passing in the correct type; and I have no recollection of ever modifying the dictionary.

SomeStudent
  • 2,856
  • 1
  • 22
  • 36
  • I can see how it is similar, i will look into it, but the first thing they mention does not seem to be my case. I am 100% positive it is not a case of foo being passed into bar. – SomeStudent May 17 '17 at 02:35
  • The code you have shown works fine. Its the code you have not shown us causing the error. Read the dupe to understand why the error occurs. –  May 17 '17 at 02:38

0 Answers0