2011-12-29 3 views
0

에 두 개체에서 매핑 할 필요가 :나는 다음과 같은 개체 모델을 가지고 하나 하나

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

내가 (내보기에 대한 구체적인) 뷰 모델을 준비하고 싶습니다. 프로젝트의

  1. 목록
  2. 페이징 정보
:

내 컨트롤러에서
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; } 
    } 
} 

, 나는이 다른 개체와를 작성하여 뷰 모델을 준비하고 싶은 다음은 뷰 모델이다

내 컨트롤러는 다음과 같습니다.

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를 사용할 수 있다는 것을 알고 있지만 이지만 여기서는 두 객체에서 단일 객체로 매핑해야합니다.

감사합니다.

+0

중복 가능성 [? 여러 집계 뿌리를 가진보기 모델을 처리하는 방법 (http://stackoverflow.com/questions/2020499/how-to- :

그런 다음 당신은 다음과 같은 코드를 사용할 수 있습니다 handle-view-model-with-multiple-aggregate-roots) –

답변

6

AutoMapper를 확장하여 여러 개체를 매핑 할 수 있습니다.

Here is a blog 일부 견본 대처법을 제공합니다.

var personViewModel = EntityMapper.Map<PersonViewModel>(person, address, comment); 
+0

+1, 아주 좋은 확장. –

+0

@Darin : 감사합니다.하지만 내 질문과 같이보기 모델에서 중첩 된 클래스를 가질 수 있습니까? 블로그에 제공된 예를 다운로드하고 하나의 중첩 된 클래스로 적응하려고 시도했지만 매핑하려고했지만 실패했습니다. – Bronzato

+0

@Darin : URL에 제공된 블로그를 살펴 보겠습니다. 예를 들어, 뷰 모델 클래스에는 일부 속성 (모두 같은 레벨에 있음)이 있지만 중첩 클래스는 없습니다. 내보기 모델에 개체가 들어있는 솔루션을 원합니다. 감사. – Bronzato

관련 문제