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.