dotnet / dotnet/aspnetcore

The model item passed into the ViewDataDictionary is of type X but this ViewDataDictionary instance requires a model item of type X.

Open
#42,127 7 comments 3 reactions 0 assignees View on GitHub
area-mvc feature-mvc-razor-views investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

The exception is thrown when running the application.

I doubled checked that the type is right, it's not an array of T, there's not duplicate types in my solution.
I found that when I comment out the model declaration in the view (`@model LicenceCountChanged` in my case) it's working fine. But I don't want to use dynamics and need strongly type model.
Also it seems like exception is thrown only when the model is in the same project with views. When I move the model to one layer higher(referenced class library) then it's also working fine

### Expected Behavior

View is rendered without an exception

### Steps To Reproduce

I cannot share my project. I'll try to create some simplified repo but it might take some time.
Here's the reproducible example:
[Issue42127.zip](https://github.com/dotnet/aspnetcore/files/8878066/Issue42127.zip)
https://github.com/koljada/Issue42127

### Exceptions (if any)

Microsoft.AspNetCore.Mvc.ViewFeatures: The model item passed into the ViewDataDictionary is of type 'PricePlanChangeFunction.LicenceCountChanged', but this ViewDataDictionary instance requires a model item of type 'PricePlanChangeFunction.LicenceCountChanged'.

### .NET Version

6.0.300

### Anything else?

I found this issue: #33636. The exception is exactly the same but I can't confirm it's related to hot reload. I disabled hot reload in my VisualStudio 17.2.3 but still have an issue. Also I'm not using preview version.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.