【发布时间】: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