通过ICollection问题预告

时间:2012-11-14 08:57:17

标签: asp.net-mvc-3 linq

我看过这个但是对于我想要达到的目标来说太复杂了: how to iterate through an Icollection In MVC3 nhibernate

我在我的控制器中:

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

  ManageUserViewModel viewModel = new ManageUserViewModel();
  ScormModule scormModule = new ScormModule();

  viewModel.User = user;
  viewModel.IsLockedOut = MembershipService.IsUserLocked(user.Email);
  viewModel.IsActivated = user.DateTimeActivated.HasValue && MembershipService.IsUserApproved(user.Email);
  viewModel.IsArchived = user.IsArchived;
  viewModel.TrainingModulesForUser = user.UserScormModules;
  foreach (int moduleId in user.UserScormModules)
  {
    var name = scormModule.GetScormModuleTitle(moduleId);
    viewModel.TrainingModuleName.Add(name);
  }

  return View(viewModel);
}

然后ManageUserViewModel:

public class ManageUserViewModel
{
 public Entities.User User { get; set; }
 public bool IsLockedOut { get; set; }
 public bool IsActivated { get; set; }
 public bool IsArchived { get; set; }\
 public ICollection<Entities.Scorm.UserScormModule> TrainingModulesForUser { get; set; }
 public List<string> TrainingModuleName { get; set; }
}

和Entities.User:

private ICollection<Entities.Scorm.UserScormModule> _userScormModules;
public virtual ICollection<Entities.Scorm.UserScormModule> UserScormModules
{
  get
  {
    if (_userScormModules == null)
      _userScormModules = new List<Entities.Scorm.UserScormModule>();
    return _userScormModules;
  }
  set
  {
    _userScormModules = value;
  }
}

和Entities.Scorm.UserScormModule:

 public class UserScormModule
 {
   public virtual int UserScormModuleId { get; set; }
   public virtual int Users_UserId { get; set; }
   public virtual int TrainingModuleArtifactScormModules_TrainingModuleArtifactId { get; set; }
   public virtual TrainingModuleArtifactScormModule TrainingModuleArtifactScormModule { get; set; }
   public virtual User User { get; set; }
 }

如何通过(int moduleId in user.UserScormModules)进行预告,以便我可以获取模块的name而不是id。我想将名字传递给我的观点

var name = scormModule.GetScormModuleTitle(moduleId);
viewModel.TrainingModuleName.Add(name);

1 个答案:

答案 0 :(得分:0)

我想我明白了,我改变了代码:

foreach (var row in user.UserScormModules)
{ 
   int moduleId = row.UserScormModuleId;
   var name = scormModule.GetScormModuleTitle(moduleId);    
   viewModel.TrainingModuleName.Add(name); 
 }