2012-11-12 2 views
1

나는 뷰 모델을 가지고 어떻게 출력합니까ViewModel에서 메서드에 액세스하는 방법?

[HttpGet] 
public ActionResult UserDetails(int id) 
{ 
    var user = ZincService.GetUserForId(id); 
    if (user == null || user.Customer.CustomerId != CurrentCustomer.CustomerId) 
    return DataNotFound(); 

    ManageUserViewModel viewModel = new ManageUserViewModel(); 
    viewModel.User = user; 
    viewModel.IsLockedOut = MembershipService.IsUserLocked(user.Email); 
    viewModel.IsActivated = user.DateTimeActivated.HasValue && MembershipService.IsUserApproved(user.Email); 
    viewModel.IsArchived = user.IsArchived; 
    viewModel.User.GetParticipatingIncentivePrograms(); 
    return View(viewModel); 
} 

:

/// Gets the incentive programs that this user is participating in. 
public IEnumerable<IncentiveProgram> GetParticipatingIncentivePrograms() 
{ 
    return Node.ParticipatingIncentivePrograms 
    .Where(x => x.PublishingState == PublishingState.Live 
     && DateTime.UtcNow.Date >= x.DateStart && DateTime.UtcNow.Date <= x.DateEnd); 
} 

그때 난 내 컨트롤러가 :

Entities.User 사용자의
public class ManageUserViewModel 
{ 
    public Entities.User User { get; set; } 
    public bool IsLockedOut { get; set; } 
    public bool IsActivated { get; set; } 
    public bool IsArchived { get; set; } 
} 

내가하는 방법이 내보기에 GetParticipatingIncentivePrograms 중? 아직 내보기에 코드가 없습니다.

감사합니다.

답변

0

Imho, 귀하의보기 모델에서 인센티브 프로그램을 대표하는 것이 좋습니다. 사실 userViewModel을 가지고 있고 아마도이 안에 프로그램 목록을 가지고있을 것입니다. 그런 다음 (폐쇄 프록시 등 접근, N + 1 DB를 호출) 몇 가지 문제로 실행할 수 있기 때문에 그것은보기에 전체 도메인 그래프를로드하지하는 것이 최선의 컨트롤러

ManageUserViewModel{ 
    public UserViewModel User { get; set; } 
} 

UserViewModel { 
    public List<string> ParticipatingIncentivePrograms { get; set; } 
} 

내에서 다음을로드합니다. 나는 당신의 프로그램을 간단한 이름 목록으로 타이핑했으나 이것을 사전이나 룩업으로 확장 할 수있다. 가능한 한 가볍고 단순한 뷰 모델을 유지하십시오.

UPDATE : 당신이 뷰 모델을 변경할 수 없습니다 당신이 값을 설정해야합니다 같이 다음 보이는

, 당신은 단지 현재 요구하고있다. 따라서 다음을 시도하십시오

viewModel.User.ParticipatingIncentivePrograms = 
     viewModel.User.GetParticipatingIncentivePrograms(); 

이러한 컬렉션이 사용자에게 있거나 사용자의보기 모델에서 다른 것으로 설정되어있는 경우이 컬렉션을 사용하십시오.

+0

이 코드가 다른 사람이 작성했으며 아키텍처를 변경할 수 없다는 점을 두려워합니다. –

관련 문제