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.
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.
Comments