[SOLVED] An unhandled exception occurred while processing the request: ViewDataDictionary is of type but this ViewDataDictionary instance requires

Issue

This Content is from Stack Overflow. Question asked by Johnny

I am working on a Web Application NET6.0 with Individual Account. After creating the application, I scaffolded the Identity items and selected to override all files. I also decided to use my existing ApplicationDbContext. Then, in my ApplicationDbContext.cs, I Created a unique Identity Schema for the default User tables.
Firthermore, I added two additional tables, Contact & InvestorRelations), with their respective ContactViewModel and InvestorRelationsModel to the list and created a View Model for each. My Override looks like this:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.HasDefaultSchema("Identity");
        builder.Entity<InvestorRelationsModel>(entity =>
        {
            entity.ToTable(name: "InvestorRelations");
        });
        builder.Entity<ContactViewModel>(entity=>
        {
            entity.ToTable(name: "Contact");
        });
        builder.Entity<IdentityUser>(entity =>
        {
            entity.ToTable(name: "User");
            entity.Property<string>("FullName").HasMaxLength(200);
        });
        builder.Entity<IdentityRole>(entity =>
        {
            entity.ToTable(name: "Role");
        });
        builder.Entity<IdentityUserRole<string>>(entity =>
        {
            entity.ToTable("UserRoles");
        });
        builder.Entity<IdentityUserClaim<string>>(entity =>
        {
            entity.ToTable("UserClaims");
        });
        builder.Entity<IdentityUserLogin<string>>(entity =>
        {
            entity.ToTable("UserLogins");
        });
        builder.Entity<IdentityRoleClaim<string>>(entity =>
        {
            entity.ToTable("RoleClaims");
        });
        builder.Entity<IdentityUserToken<string>>(entity =>
        {
            entity.ToTable("UserTokens");
        });

    }

I created a new migration and updated the database without errors. However, when I ran the pplication, and navigated to the Register page, The following error message appeared:

An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type '_200Blue.Areas.Identity.Pages.Account.RegisterModel', but this ViewDataDictionary instance requires a model item of type '_200Blue.Models.ContactViewModel'.

I am not sure how to fix this error. I did not modify the Register.cshtml.cs file and only have the one Models/ContactViewModel.cs that I created to save my Web Emails into the Database.

Any assistance or recommendations would be appreciated.



Solution

After searching everywhere without success, I decided to trace my steps from the time that the Web site was working; I discovered that I added a model to the default layout page that was influencing and overwriting the Registry Model. Once I reengineered the Default Layout template and removed the ContactViewModel from it, then the Register page loaded as required.


This Question was asked in StackOverflow by Johnny and Answered by Johnny It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?