2012-11-14 63 views
0

我看了一下這一點,但太複雜,我想實現: how to iterate through an Icollection In MVC3 nhibernate的foreach槽ICollection的問題

我在我的控制器:

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

and 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)的foreach這樣我就可以得到模塊,而不是idname。我想的名字傳遞給我的看法

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

我想我得到了它,我改變了代碼:用戶的foreach(VAR行.UserScormModules) { int moduleId = row.UserScormModuleId; var name = scormModule.GetScormModuleTitle(moduleId); viewModel.TrainingModuleName.Add(name); } –

+0

請將此轉換爲答案。 –

回答

0

我想我得到了它,我改變了代碼:

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