2017-12-19 8 views
-4

私はエラーが発生しています: 'int'に 'GetEnumerator'のパブリック定義が含まれていないため、foreach文は 'int'型の変数では動作できません。getenumerator mvcのパブリック定義が存在するため、foreach文は 'int'型の変数に対しては機能しません。

public ActionResult RoleCreate() 
{ 
userType type = new userType(); 
List<DomainView> EmpList = type.GetAllRoleModulesViews(); 
List<ViewRoleModules> modules = new List<ViewRoleModules>(); 
Role objBind = new Role(); 
objBind.DomainViews = EmpList; 
foreach (DomainView emp in EmpList) 
{ 
    int modID = emp.DomainID; 
    foreach (ViewRoleModules mod in modID) 
    { 

    } 
} 

return View(objBind); 
} 

モジュール:

public class DomainView 
    { 
     [Key] 
     public int DomainID { get; set; } 
     public string DomainCode { get; set; } 
     public string DomainName { get; set; } 
     public int TabOrder { get; set; } 
     public string UserName { get; set; } 
     public int CreatedBy { get; set; } 
     public DateTime CreatedDate = DateTime.UtcNow; 
     public int ModifiedBy { get; set; } 
     public DateTime ModifiedDate = DateTime.UtcNow; 

     public bool IsChecked { get; set; } 

     public IEnumerable<DomainView> DomainViews { get; set; } 
    } 

public class ViewRoleModules 
    { 
     [Key] 
     public int ModuleID { get; set; } 
     public int DomainID { get; set; } 
     public int ParentModuleID { get; set; } 
     public int CompanyTypeID { get; set; } 
     public string ModuleName { get; set; } 
     public string FolderName { get; set; } 
     public string Url { get; set; } 
     public int TabOrder { get; set; } 
     public string Style { get; set; } 
     public string Status { get; set; } 
     public string IsTab { get; set; } 
     public string ApprovalProcess { get; set; } 
     public int CreatedBy { get; set; } 
     public DateTime CreatedDate = DateTime.UtcNow; 
     public int ModifiedBy { get; set; } 
     public DateTime ModifiedDate = DateTime.UtcNow; 

     public string DomainName { get; set; } 
     public string RoleName { get; set; } 
     public int RoleID { get; set; } 

     public bool IsChecked { get; set; } 

     public IEnumerable<ViewRoleModules> ModuleViews { get; set; } 
    } 

tittle

+0

エラーメッセージはcです学習する。 modIDは整数であり、整数を反復することはできません。 –

+0

私は 'ViewRoleModules'にID値を渡す必要があります@AndyG –

+2

[ask]を読んで、あなたがしようとしているものの[mcve]を作成してください。これはXYの問題です。エンティティのリストがあり、それを別のエンティティなどにマップしたい場合、 'foreach(var x in someInt) 'はそれを実行しません。 – CodeCaster

答えて

1

変化として以下のコード:

foreach (ViewRoleModules mod in modules) 
{ 
} 
あなたが代わりに modules modIdmodIdを使用している

intとないあなたはまだいくつかの出力を得るためにmodulesコレクションを移入する必要が収集

です。

+0

モジュールを 'new List <>'に初期化しました。それは何も含まれないので、この提案は解決策ではありません。はい、それは範囲にある列挙型ですが、それはそれについてです。 – CodeCaster

+0

はい@コードキャスター... –

0

あなたはArrayやListやDictionaryのようなコレクションでそれぞれに使用できます。

よう 例が、あなたのコードから、それはあなたがこのエラーが発生したため、コレクションとしてint型の変数を反復処理できなかった単一のint変数

int modID = emp.DomainID; 

です。

public class DomainView 
    { 
     public int DomainID { get; set; } 
     public string DomainCode { get; set; } 
     public string DomainName { get; set; } 
     public int TabOrder { get; set; } 
     public string UserName { get; set; } 
     public int CreatedBy { get; set; } 
     public DateTime CreatedDate = DateTime.UtcNow; 
     public int ModifiedBy { get; set; } 
     public DateTime ModifiedDate = DateTime.UtcNow; 

     public bool IsChecked { get; set; } 

     public IEnumerable<DomainView> DomainViews { get; set; } 

     public IEnumerable<ViewRoleModules> ModuleViews { get; set; } 
    } 

とループの

foreach (DomainView emp in EmpList) 
     { 
      foreach (ViewRoleModules mod in emp.ModuleViews) 
      { 

      } 
     } 

のようにDomainViewクラスを変更するが、必ずデータベースをリンクした作りとためViewRoleModulesを得る所望の出力を得るために私の理解から

ビジネスロジック内のコードを使用した特定のDomainView

type.GetAllRoleModulesViews();

+0

私のコードで編集できますか? –

+0

は働いていません。 –

関連する問題