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

问题描述 投票:-4回答:2

在这一行我收到一个错误: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

c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3
2个回答
1
投票

更改代码如下:

foreach (ViewRoleModules mod in modules)
{ 
}

您正在使用modId而不是modules modIdint而不是集合

你仍然需要填充modules集合来获得一些输出。


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();

© www.soinside.com 2019 - 2024. All rights reserved.