Skip to content

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

Open
@koljada

Description

@koljada

Is there an existing issue for this?

  • 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/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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-mvc-razor-viewsFeatures related to the Razor view engine for Razor pages and MVC viewsinvestigate

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions