c# - MVC Entity Framework 在 View 中使用嵌套的 foreach

标签 c# asp.net-mvc entity-framework

我将 3 组数据传递给 View 模型中的一个 View ,并且在我的 foreach 编码中将其中一组与另外两组相关联时遇到问题:

我的数据库表是: “服务组”是“服务”的父级 “服务”通过连接表(使用 2 复合主键)与 aspnet_users 表(我称为“用户”)相关

我将其放入名为 GWServices.edmx 的 EF 模型中

所以现在我有 3 个相关的实体,如下所示: ServiceGroup(父)服务 服务(多对多)用户

然后我像这样创建了一个 Controller 和一个 View 模型:

{

    public class ServicesViewModel
    {
        public ServicesViewModel(List<ServiceGroup> servicegroups, List<Service> services, List<User> aspnetusers)
        {
            this.ServiceGroups = servicegroups;
            this.Service = services;
            this.AspnetUsers = aspnetusers;

        }

        public List<ServiceGroup> ServiceGroups { get; set; }
        public List<Service> Service { get; set; }
        public List<User> AspnetUsers { get; set; }

    }


    public class ClientServicesController : Controller
    {

        public ActionResult Index()
        {
            GWEntities _db = new GWEntities();

            var servicegroups = _db.ServiceGroupSet.ToList();
            var services = _db.ServiceSet.ToList();
            var aspnetusers = _db.UserSet.ToList();

            return View(new ServicesViewModel(servicegroups, services, aspnetusers));
        }

    }
}

接下来我用 3 个 foreach 循环创建了 View :

  1. 获取并呈现为 UL,数据库中所有服务组的列表(有效)
  2. 在每个服务组下填充一个表格,填写该给定组内每个服务的一些服务数据(这也有效)
  3. 测试用户是否与该特定服务相关联,如果是,则仅显示红色十字图标,否则显示绿色“选择我”图标。 (这不会填充任何用户)

所以代码看起来是这样的:

<% foreach (var servgroup in Model.ServiceGroups) { %> 
<ul> <%= servgroup.ServiceGroupName%> </ul>
<table>
     <% foreach (var serv in servgroup.Service)
        { %>
     <tr>
     <td class="td1">
     <%= serv.ServiceDescription%>
     </td>
     <td class="td2">
     <% = Html.Encode(String.Format("{0:f}",serv.MonthlyPrice)) %> 
        </td>
        <td class="td3"> 
           <%foreach (var user in serv.User) {%>
              <%if (user.UserName == User.Identity.Name)
                { %>
            <img src="/Content/LightRedCross_2.png" alt="" height="15px" width="15px"/>
                    <% }
                else
                {%>
            <img src="/Content/LightGreenAdd_2.png" alt="" height="15px" width="15px"/>           
              <%} %>
           <%} %>
        </td>
        </tr>
   <% } %> 
    </table> 
<% } %>

谁能告诉我为什么 foreach(serv.user 中的 var 用户)无法识别“Customer1”(当前登录的用户)订购了 6 项服务(因为它在连接表中)?

谢谢!

保罗

最佳答案

查看您的代码,我认为这可以通过加载 ServiceGroups 的子表来解决。

服务和用户引用可能未加载到原始 Entity Framework 查询中。因此,当您尝试遍历 foreach 循环中的子项时,它看不到集合,它只看到 null。

你应该尝试改变你的检索语句:

_db.ServiceGroupSet.ToList()

_db.ServiceGroupSet.Include("ServiceSet").Include("UserSet").ToList()

如果这不能解决您的问题,我会在下面的代码行中放置一个断点并遍历列表以查看它是否包含您期望的数据。

this.ServiceGroups = servicegroups; 

关于c# - MVC Entity Framework 在 View 中使用嵌套的 foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2040088/

相关文章:

c# - JavaScript : how do I prevent a user from selecting anything in a select option list without using disable/pointer-events?

c# - ASP.NET MVC 上传文件超时

c# - 选择nhibernate二级缓存方案时考虑的因素有哪些

entity-framework - EF6 无法为表拆分/共享主键 + 基类构建模型?

mysql - mysql中的EF自引用表

c# - 使用导航属性获取数据

c# - .NET - 限制执行单​​元的实例数

c# - 在 Func<> 上定义代码契约

c# - DisplayNameFor 用于从 IEnumerable<T> 派生的接口(interface)

jquery - 使用 asp.net core 删除所需的验证