The model item passed into the ViewDataDictionary is of type 'College.WebInterface.Models.ViewModels.HomeIndexViewModel', but this ViewDataDictionary instance requires a model item of type 'College.WebInterface.Models.Department'.

MH Khan 21 Reputation points
2022-11-17T06:33:22.51+00:00

I am getting the error even after using correct objects and models and namespaces

///Controller code
public IActionResult Index()
{
HomeIndexViewModel HomePageModel = new HomeIndexViewModel();
//List<Notification> nf = new List<Notification>();
var f = (from n in _context.Notifications.OrderByDescending(n => n.Date) select n).Take(5);
HomePageModel.Notifications.AddRange(f);
return View(HomePageModel);
}

///Razor-View Code
@默 College.WebInterface.Models.ViewModels.HomeIndexViewModel
<h1> welcome to index page</h1>

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Accepted answer
  1. Anonymous
    2022-11-21T06:51:51.963+00:00

    Hi @MH Khan ,

    Okay, I found it. The issue relates the _layout.cshtml page, there has a page model at the header. After removing it, the code works well:

    image 1

    The result as below:

    262425-image.png

    [Note] To protect your project, if the problem solved, please remember to delete the comment which contains the Github repo link.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MH Khan 21 Reputation points
    2022-11-21T09:41:44.17+00:00

    Thankxyou @Anonymous .. the issue is resolved now

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.