【问题标题】:foreach statement cannot operate on variables of type 'int' because public definition for getenumerator mvc?foreach 语句不能对“int”类型的变量进行操作,因为 getenumerator mvc 的公共定义?
【发布时间】:2018-06-01 17:14:01
【问题描述】:

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

【问题讨论】:

  • 错误信息很清楚。 modID 是整数,不能迭代整数。
  • 我能做什么我需要将 ID 值传递给ViewRoleModules@AndyG
  • 阅读How to Ask 并创建一个minimal reproducible example 你正在尝试做的事情。这是一个 XY 问题。您有一个实体列表,并希望将其映射到另一个实体或其他实体,而您的 foreach (var x in someInt) 不会这样做。
  • 我认为你应该通过 emp
  • 我们不知道emp.DomainID 和 ViewRoleModules 之间的联系。

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


【解决方案1】:

您可以在数组、列表或字典等集合中使用每个。 比如

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

【讨论】:

    【解决方案2】:

    修改代码如下:

    foreach (ViewRoleModules mod in modules)
    { 
    }
    

    您使用的是modId 而不是modules modIdint 而不是集合

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

    【讨论】:

    • 他们只是将模块初始化为new List&lt;&gt;。它不会包含任何内容,因此此建议不是解决方案。是的,它是一个在范围内的可枚举,但仅此而已。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2016-03-27
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多