"The model item passed into the dictionary is of type" while passing model to a partial view

Nuru Salihu

I have a view renders another partial view along the way. My view receives the model below

 public class JobsListViewModel
    {
        public IEnumerable<JobPost> JobPosts { get; set; }
        public PagingInfo PagingInfo { get; set; }
        public SearchTerms searchTerms { get; set; }
    }

my Partial view receives the model SearchTerms below.

  public class SearchTerms
    {
        public string searchText { get; set; }
        public string JobFunction { get; set; }
        public string JobIndustry { get; set; }
        public string jobType { get; set; }
        public string JobLevel { get; set; }
        public string PostedDate { get; set; }
        public decimal MinSalary { get; set; }
    }

SearchTerms already part of JobsListViewModel .Therefore i tried to render my Partial view like below.

@Html.Partial("_SearchFormPartial",Model.searchTerms)

The above throws an error that

"The model item passed into the dictionary is of type 'JobWebSite.WebUI.Models.JobsListViewModel', but this dictionary requires a model item of type 'JobWebSite.WebUI.Models.SearchTerms' "

From my partial above, i am passing Model.SearchTerms. Shouldnt that satisfy the requirement ? Please any help would be appreciated.

Edit

Thank you for your responses. What Mr Stephen mentioned below happened to be the problem. Below is my Controller previously.

 [HttpGet]
    public ViewResult List(int page = 1)
     {
     JobPosts = (IEnumerable<JobPost>)
                          (from posts in repository.JobPosts
                           orderby posts.PostDate descending
                           select new
                           {
                               Id = posts.Id,
                               Post = posts.Post,
                               Logo = posts.Logo,
                               PostDate = posts.PostDate,
                               EmployerId = posts.EmployerId,
                               CategoryId = posts.CategoryId,
                               RegionId = posts.RegionId,
                               TypeId = posts.TypeId,
                               PostTitle = posts.PostTitle,
                               JobIndustryId = posts.JobIndustryId,
                               JobFunctionId = posts.JobFunctionId,
                               JobLevelId = posts.JobLevelId,
                               Salary = posts.Salary
                           }).AsEnumerable().Select(x => new JobPost
                               {
                                   Id = x.Id,
                                   Post = x.Post,
                                   Logo = x.Logo,
                                   PostDate = x.PostDate,
                                   EmployerId = x.EmployerId,
                                   CategoryId = x.CategoryId,
                                   RegionId = x.RegionId,
                                   TypeId = x.TypeId,
                                   PostTitle = x.PostTitle,
                                   JobIndustryId = x.JobIndustryId,
                                   JobFunctionId = x.JobFunctionId,
                                   JobLevelId = x.JobLevelId,
                                   Salary = x.Salary
                               })
                               .Skip((page - 1) * PageSize)
                               .Take(PageSize),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = page,
                        ItemsPerPage = PageSize,
                        TotalItems = repository.JobPosts.Count()
                    },
                    searchTerms = CtrlsearchTerms 


                };


     return View("Search", model);
    }

Note. CtrlsearchTerms is set in the httpPost method. According to Mr Stephen , My SearchTerms was empty and that sure was the case. when i changed my Controller to

[HttpGet]
        public ViewResult List(int page = 1)
         {
         JobPosts = (IEnumerable<JobPost>)
                              (from posts in repository.JobPosts
                               orderby posts.PostDate descending
                               select new
                               {
                                   Id = posts.Id,
                                   Post = posts.Post,
                                   Logo = posts.Logo,
                                   PostDate = posts.PostDate,
                                   EmployerId = posts.EmployerId,
                                   CategoryId = posts.CategoryId,
                                   RegionId = posts.RegionId,
                                   TypeId = posts.TypeId,
                                   PostTitle = posts.PostTitle,
                                   JobIndustryId = posts.JobIndustryId,
                                   JobFunctionId = posts.JobFunctionId,
                                   JobLevelId = posts.JobLevelId,
                                   Salary = posts.Salary
                               }).AsEnumerable().Select(x => new JobPost
                                   {
                                       Id = x.Id,
                                       Post = x.Post,
                                       Logo = x.Logo,
                                       PostDate = x.PostDate,
                                       EmployerId = x.EmployerId,
                                       CategoryId = x.CategoryId,
                                       RegionId = x.RegionId,
                                       TypeId = x.TypeId,
                                       PostTitle = x.PostTitle,
                                       JobIndustryId = x.JobIndustryId,
                                       JobFunctionId = x.JobFunctionId,
                                       JobLevelId = x.JobLevelId,
                                       Salary = x.Salary
                                   })
                                   .Skip((page - 1) * PageSize)
                                   .Take(PageSize),
                        PagingInfo = new PagingInfo
                        {
                            CurrentPage = page,
                            ItemsPerPage = PageSize,
                            TotalItems = repository.JobPosts.Count()
                        },
                         //Initialize my SearchTerms Below as suggested
                        searchTerms = new SearchTerms()


                    };


         return View("Search", model);
        }

This works fine. The error is now gone. Thank you for your time.

user3559349

This occurs when the model you are passing to the partial is null. In effect your doing @Html.Partial("_SearchFormPartial", null) which is the same as @Html.Partial("_SearchFormPartial"), that is, the ViewDaatDictionary of the main view (JobsListViewModel) is being passed to the partial. Initialize a new instance of SearchTerms in the controller or in a parameterless constructor, for example

public class JobsListViewModel
{
    public JobsListViewModel()
    {
        searchTerms = new SearchTerms();
    }
    public IEnumerable<JobPost> JobPosts { get; set; }
    public PagingInfo PagingInfo { get; set; }
    public SearchTerms searchTerms { get; set; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"The model item passed into the dictionary is of type" while passing model to a partial view

From Dev

The model item passed into the dictionary is of type view model but requires generic IEnum

From Dev

The model item passed into the dictionary is of type ..., but this dictionary requires ...

From Dev

The model item passed into the dictionary is of type error

From Java

The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

From Dev

The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' '

From Dev

How to fix The model item passed into the dictionary is of type error?

From Dev

The model item passed into the dictionary is of type 'System.Net.Http.HttpResponseMessage',

From Dev

How to fix The model item passed into the dictionary is of type error?

From Dev

The model item passed into the dictionary is of type 'System.Linq.Enumerable

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.DbSet

From Dev

MVC: The model item passed into the dictionary is of type System.Int32

From Dev

The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'

From Dev

The model item passed into the dictionary is of type 'BlogHomePageModel', but this dictionary requires a model item of type 'BlogHomePageModel'

From Dev

The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'X'

From Dev

How to handle The model item passed into dictionary is type of LoginViewModel, but this dictionary requires a model type of RegisterViewModel?

From Dev

model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type

From Dev

The model item passed into the dictionary is of type 'a', but this dictionary requires a model item of type 'b'

From Dev

model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'

From Dev

The model item passed into the dictionary is of type 'System.Collections.Generic.List... in ASP.net MVC

From Dev

ASP.Net MVC "The model item passed into the dictionary is of type 'System.Collections.Generic.List"

From Dev

The model item passed into the dictionary is of type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`

From Dev

MVC Razor The model item passed into the dictionary is of type List String, but this dictionary requires IEnumerable

From Dev

Create ActionResult for save throws error saying The model item passed into the dictionary is of type

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery

From Dev

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`

From Dev

The model item passed into the dictionary is of type 'System.Collections.Generic.List in Razor mvc5

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

From Dev

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[X]', but this dictionary requires a model item of type 'X'

Related Related

  1. 1

    "The model item passed into the dictionary is of type" while passing model to a partial view

  2. 2

    The model item passed into the dictionary is of type view model but requires generic IEnum

  3. 3

    The model item passed into the dictionary is of type ..., but this dictionary requires ...

  4. 4

    The model item passed into the dictionary is of type error

  5. 5

    The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

  6. 6

    The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' '

  7. 7

    How to fix The model item passed into the dictionary is of type error?

  8. 8

    The model item passed into the dictionary is of type 'System.Net.Http.HttpResponseMessage',

  9. 9

    How to fix The model item passed into the dictionary is of type error?

  10. 10

    The model item passed into the dictionary is of type 'System.Linq.Enumerable

  11. 11

    The model item passed into the dictionary is of type 'System.Data.Entity.DbSet

  12. 12

    MVC: The model item passed into the dictionary is of type System.Int32

  13. 13

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'

  14. 14

    The model item passed into the dictionary is of type 'BlogHomePageModel', but this dictionary requires a model item of type 'BlogHomePageModel'

  15. 15

    The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'X'

  16. 16

    How to handle The model item passed into dictionary is type of LoginViewModel, but this dictionary requires a model type of RegisterViewModel?

  17. 17

    model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type

  18. 18

    The model item passed into the dictionary is of type 'a', but this dictionary requires a model item of type 'b'

  19. 19

    model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'

  20. 20

    The model item passed into the dictionary is of type 'System.Collections.Generic.List... in ASP.net MVC

  21. 21

    ASP.Net MVC "The model item passed into the dictionary is of type 'System.Collections.Generic.List"

  22. 22

    The model item passed into the dictionary is of type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`

  23. 23

    MVC Razor The model item passed into the dictionary is of type List String, but this dictionary requires IEnumerable

  24. 24

    Create ActionResult for save throws error saying The model item passed into the dictionary is of type

  25. 25

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery

  26. 26

    Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`

  27. 27

    The model item passed into the dictionary is of type 'System.Collections.Generic.List in Razor mvc5

  28. 28

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

  29. 29

    The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[X]', but this dictionary requires a model item of type 'X'

HotTag

Archive