如何在 asp.net MVC 4 中使用子 table 中的数据显示父 table 的数据

How to display the data of the parent table using the data in the child table in asp.net MVC 4

我正在尝试用 C# 创建一个 Web 应用程序 Asp.net mcv 4.

我处于必须根据 table "ReadNotifs".[=20 中的数据显示 table "Notifications" 的数据的级别=]

为了说明问题,这里有更多细节:

我的数据库中有两个 table:通知和 ReadNotifs。

Table : Notifications            Table : ReadNotifs
--------------------------      --------------------------
|NotificationID | int    |      |ReadNotifID    | int    |
|description    | string |      |userid         | int    |
|UserId         | int    |      |NotificationID | int    |
__________________________      __________________________

Notifications 和 ReadNotifs tables 由一对多关系链接。所以1个Notification可以多次读取(ReadNotif)

我希望我能得到这个结果:

Table : Notifications               Table : ReadNotifs
----------------------------------- -----------------------------------
|NotificationID|description|UserId| |ReadNotifID|userid|NotificationID|
|      1       | Post 1    |  50  | |     1     |  50  |      3       |
|      2       | Post 2    |  51  | |     2     |  50  |      1       |
|      3       | Post 3    |  52  | ___________________________________
|      4       | Post 4    |  53  | 
___________________________________ 

Result that i want to display in my view:

Table : Notifications              
----------------------------------- 
|NotificationID|description|UserId|  
|      2       | Post 2    |  51  |
|      4       | Post 4    |  53  | 
___________________________________ 

记住:我必须根据 table "ReadNotifs" 中的数据在 table "Notifications" 中显示数据。 所以,我想在 NotificationID(of Notifications) != NotificationID (ReadNotifs).

时显示通知

知道我应该如何进行吗?

非常感谢。

编辑:大家好。我尝试了一些东西:

我的视图模型:

public class NotificationVM
{
    public ReadNotif ReadNotif { get; set; }
    public List<Notification> Notifications { get; set; }
}

我的控制器:

UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName == User.Identity.Name); 

var listreadnotifs = db.ReadNotifs
                                 //.Where(u => u.userid == user.UserId)
                                     .ToList();

                if (listreadnotifs != null)
                {
                    List<NotificationVM> result = new List<NotificationVM>();

                    foreach (var item in listreadnotifs)
                    {
                        NotificationVM model = new NotificationVM();
                        //model.ReadNotif = item;
                        model.Notifications = db.Notifications
                            .OrderByDescending(i => i.NotificationID)
                            .Where(u => !(u.NotificationID == item.NotificationID))
                            .Take(99)
                            .ToList();

                        result.Add(model);

                    }
                    return PartialView(result);
                }

我的看法:

 @foreach (var beta in Model)
    {
        foreach (var item in beta.Notifications)
        {
            @item.description
        }
    }

当我应用这个方法时,让我用最后一个例子告诉你结果:

Table : Notifications               Table : ReadNotifs
----------------------------------- -----------------------------------
|NotificationID|description|UserId| |ReadNotifID|userid|NotificationID|
|      1       | Post 1    |  50  | |     1     |  50  |      3       |
|      2       | Post 2    |  51  | |     2     |  50  |      1       |
|      3       | Post 3    |  52  | ___________________________________
|      4       | Post 4    |  53  | 
___________________________________ 

Result :

(i have this result)                    (instead of this one)
Table : Notifications                   Table : Notifications
-----------------------------------     -----------------------------------
|NotificationID|description|UserId|     |NotificationID|description|UserId|
|      1       | Post 1    |  50  |     |      2       | Post 2    |  51  |
|      2       | Post 2    |  51  |     |      4       | Post 4    |  53  |
|      4       | Post 4    |  53  |     ___________________________________
___________________________________ 

所以这个解决方案部分有效。

有什么想法吗?

正如我从您的代码中看到的那样,最好的方法是在您的控制器方法中使用 "include",如下所示:

var notifs = db.ReadNotifs.Include(a => a.Notifications);
        return View(notifs);

要生成除在 ReadNotifs 中有相应记录(即未读通知)之外的所有 Notifications 的集合,您可以使用以下查询

List<Notification> unreadNotifications = db.Notifications
  .Where(n => !db.ReadNotifs.Any(r => r.NotificationID == n.NotificationID));

旁注:目前尚不清楚 ReadNotif 属性 在您的视图模型中的用途(您从未在任何地方设置它),因此似乎没有必要。您可以在视图中制作模型 @model List<Notification>