我有以下實體模型:需要從兩個對象映射到一個單一的一個
public class Project
{
[Key]
public int ProjectID { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Content { get; set; }
public string Category { get; set; }
public string Client { get; set; }
public int Year { get; set; }
// more attributes here...
}
我想準備(具體爲我的視圖)視圖模型。這裏是視圖模型:
public class ProjectListViewModel
{
public IEnumerable<ProjectInfos> ProjectList { get; set; }
public PagingInfo Paging { get; set; }
public class ProjectInfos
{
public string Title { get; set; }
public string Slug { get; set; }
public string Content { get; set; }
public string Category { get; set; }
public string Client { get; set; }
public int Year { get; set; }
}
public class PagingInfo
{
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages { get; set; }
}
}
在我的控制,我想用2個不同的對象填充它編寫視圖模型:
- 的項目清單
- 尋呼信息
這裏是我的控制器:
public ViewResult List(string category, int page = 1)
{
IEnumerable<Project> projectList = m_Business.GetProjects(category, page, 10);
PagingInfo pagingInfo = m_Business.GetPagingInfo(category, page, 10);
// Here I need to map !!
ProjectListViewModel viewModel = .....
return View(viewModel);
}
那麼我該如何在我的控制器中進行操作?我知道我們可以使用automapper從一個對象映射到另一個對象,但在這裏我需要從兩個對象映射到一個對象。
謝謝。
可能重複[?如何處理與多個集合根視圖模型(http://stackoverflow.com/questions/2020499/how-to-處理視圖模型與多個聚合根) – 2011-12-29 14:25:18