0

EDIT: I haven't found solution to my problem, but i think to this minute that i was not wrong but somehow VS was bugging or me at least.

So i created new project from scratch ( since it is small project just for exercise and did all from the start. Now it is working fine :) so i have no concrete solution for this, i tried everything what you gave me, thank you for your time :)

So I have 2 simple controllers and 2 views. I also have two ViewModels for them separate.

When I try to load Index.cshtml this error occurs:

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

This is my view model.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;

namespace Vidly.ViewModels
{
    public class CustomerViewModel
    {
        public List<Customer> Customers { get; set; }
    }
}

This is my view:

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

<h2>Index</h2>

And a controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;
using Vidly.ViewModels;

namespace Vidly.Controllers
{
    public class CustomersController : Controller
    {
        // GET: Customers

        public ActionResult Index()
        {
            var customers = new List<Customer>
            {
                new Customer {Id = 1, Name = "Willy Wonka"},
                new Customer {Id= 2 , Name = "Walter White"}
            };


            var viewModel = new CustomerViewModel
            {
                Customers = customers
            };

            return View(viewModel);
        }
    }
}

However, I can't find why does it load this model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;

namespace Vidly.ViewModels
{
    public class RandomMovieViewModel
    {
        public Movie Movie { get; set; }
    }
}
Blazo
  • 95
  • 1
  • 2
  • 13
  • What is your `route` ? Is that `Customers/Index` ? – Rahul Nov 15 '18 at 16:45
  • Yeah it is, however i haven't set route anywhere for this since it is just this part of code for now. Forgive if i tell confusely I am a beginner. – Blazo Nov 15 '18 at 16:53
  • Have you tried to clean / rebuild the solution? Sometimes Visual Studio f***s up and is not running the most recent version of your code. Restarting Visual Studio may help too. – johey Nov 15 '18 at 16:58
  • Just did that, still no luck :/ – Blazo Nov 15 '18 at 17:04

1 Answers1

0

What's the name of your view file? I think it's loading the wrong view. Your action name is Index, so your view file must be Index.cshtml in a folder named Customers.

Mahmood Dehghan
  • 7,761
  • 5
  • 54
  • 71
  • It is all like you said, under the right folder with Index name – Blazo Nov 15 '18 at 16:51
  • And what's the name of the other view? and is there another action in your controller? – Mahmood Dehghan Nov 15 '18 at 16:57
  • Other is Random.cshtml as the action in other controller is under Random() method , no there is not – Blazo Nov 15 '18 at 17:01
  • That other View is opening without a problem with his RandomViewModel, but before i created this new Model for Customer i had in RandomViewModel property of type Customer but then i deleted it since i wanted separate now. Is there something that may help you? – Blazo Nov 15 '18 at 17:09