2017-12-19 4 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

예 @CodeCaster ... –

0

배열이나 목록 또는 사전과 같은 컬렉션에서 각각 사용할 수 있습니다.

List<int> intList = new List<int>(); 

좋아하지만 코드에서 예, 그것은 때문에이 오류가 발생, 단일의 int 변수

int modID = emp.DomainID; 

당신이 모음으로 INT 변수를 반복 할 수없는 것입니다. 나의 이해에서

원하는 출력이

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

로 DomainView 클래스를 변경 얻을에 루프에 대한

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

      } 
     } 

하지만 데이터베이스를 연결했는지 확인하고 대한 ViewRoleModules를 얻을 수 비즈니스 로직 내부의 코드를 사용하는 특정 DomainView

type.GetAllRoleModulesViews();

+0

내 코드에서 편집 할 수 있습니까? –

+0

작동하지 않습니다. –

관련 문제