我有一个视图在此过程中呈现另一个局部视图。我的观点接收下面的模型
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)
以上引发错误
&#34;传递到字典中的模型项的类型为&#39; JobWebSite.WebUI.Models.JobsListViewModel&#39;,但此字典需要类型为&#39; JobWebSite.WebUI.Models的模型项.SearchTerms&#39; &#34;
从我上面的部分内容来看,我正在传递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方法中设置。根据斯蒂芬先生的说法,我的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);
}
这很好用。错误现在消失了。谢谢你的时间。
答案 0 :(得分:1)
当您传递给partial的模型为null
时,会发生这种情况。实际上,与@Html.Partial("_SearchFormPartial", null)
相同的行为@Html.Partial("_SearchFormPartial")
,即主视图ViewDaatDictionary
(JobsListViewModel
)正在传递给部分。在控制器或无参数构造函数中初始化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; }
}