"The model item passed into the dictionary is of type" 将模型传递给局部视图
"The model item passed into the dictionary is of type" while passing model to a partial view
我有一个视图正在渲染另一个局部视图。我的看法收到下面的模型
public class JobsListViewModel
{
public IEnumerable<JobPost> JobPosts { get; set; }
public PagingInfo PagingInfo { get; set; }
public SearchTerms searchTerms { get; set; }
}
我的分部视图收到以下模型 SearchTerms。
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 已经是 JobsListViewModel 的一部分。因此我尝试如下所示呈现我的部分视图。
@Html.Partial("_SearchFormPartial",Model.searchTerms)
以上抛出一个错误
"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' "
从我上面的部分来看,我超过了 Model.SearchTerms。那不应该满足要求吗?请提供任何帮助。
编辑
感谢您的回复。斯蒂芬先生在下面提到的恰好是问题所在。下面是我之前的控制器。
[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);
}
注意。 CtrlsearchTerms 在 httpPost 方法中设置。根据 Stephen 先生的说法,My SearchTerms 是空的,事实确实如此。当我将控制器更改为
[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);
}
这很好用。现在错误消失了。谢谢你的时间。
当您传递给局部的模型是 null
时会发生这种情况。实际上,您所做的 @Html.Partial("_SearchFormPartial", null)
与 @Html.Partial("_SearchFormPartial")
相同,即主视图 (JobsListViewModel
) 的 ViewDaatDictionary
正在传递给局部视图。在控制器或无参数构造函数中初始化 SearchTerms
的新实例,例如
public class JobsListViewModel
{
public JobsListViewModel()
{
searchTerms = new SearchTerms();
}
public IEnumerable<JobPost> JobPosts { get; set; }
public PagingInfo PagingInfo { get; set; }
public SearchTerms searchTerms { get; set; }
}
我有一个视图正在渲染另一个局部视图。我的看法收到下面的模型
public class JobsListViewModel
{
public IEnumerable<JobPost> JobPosts { get; set; }
public PagingInfo PagingInfo { get; set; }
public SearchTerms searchTerms { get; set; }
}
我的分部视图收到以下模型 SearchTerms。
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 已经是 JobsListViewModel 的一部分。因此我尝试如下所示呈现我的部分视图。
@Html.Partial("_SearchFormPartial",Model.searchTerms)
以上抛出一个错误
"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' "
从我上面的部分来看,我超过了 Model.SearchTerms。那不应该满足要求吗?请提供任何帮助。
编辑
感谢您的回复。斯蒂芬先生在下面提到的恰好是问题所在。下面是我之前的控制器。
[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);
}
注意。 CtrlsearchTerms 在 httpPost 方法中设置。根据 Stephen 先生的说法,My SearchTerms 是空的,事实确实如此。当我将控制器更改为
[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);
}
这很好用。现在错误消失了。谢谢你的时间。
当您传递给局部的模型是 null
时会发生这种情况。实际上,您所做的 @Html.Partial("_SearchFormPartial", null)
与 @Html.Partial("_SearchFormPartial")
相同,即主视图 (JobsListViewModel
) 的 ViewDaatDictionary
正在传递给局部视图。在控制器或无参数构造函数中初始化 SearchTerms
的新实例,例如
public class JobsListViewModel
{
public JobsListViewModel()
{
searchTerms = new SearchTerms();
}
public IEnumerable<JobPost> JobPosts { get; set; }
public PagingInfo PagingInfo { get; set; }
public SearchTerms searchTerms { get; set; }
}