foreach语句不能对'int'类型的变量进行操作,因为getenumerator mvc的公共定义?

时间:2017-12-19 09:45:52

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

在这一行我得到一个错误:foreach语句不能对'int'类型的变量进行操作,因为'int'不包含'GetEnumerator'的公共定义。

 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

2 个答案:

答案 0 :(得分:1)

更改代码如下:

foreach (ViewRoleModules mod in modules)
{ 
}

您使用modId代替modules modIdint而不是集合

您仍然需要填充modules集合才能获得一些输出。

答案 1 :(得分:0)

你可以在像Array或List或Dictionary这样的集合中使用它们。 例如

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

但是从你的代码中它是一个单独的int变量

int modID = emp.DomainID;

您无法将int变量作为集合进行迭代,因此会收到此错误。

根据我的理解获得所需的输出将DomainView类更改为

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

和for循环

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

            }
        }

但请确保您已使用业务逻辑中的代码链接数据库并获取特定DomainView的ViewRoleModules

  

type.GetAllRoleModulesViews();