简体   繁体   中英

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

I am trying to make a web application in C # Asp.net mcv 4.

I'm at the level where I would have to show data of a table "Notifications" based on the data in a table "ReadNotifs".

To illustrate the problem, here are more details:

I have two tables in my database: Notifications and ReadNotifs.

Table : Notifications            Table : ReadNotifs
--------------------------      --------------------------
|NotificationID | int    |      |ReadNotifID    | int    |
|description    | string |      |userid         | int    |
|UserId         | int    |      |NotificationID | int    |
__________________________      __________________________
  • These are the related models :

      public class Notification { public Notification() { this.ReplyNotifs = new HashSet<ReplyNotif>(); } [Key] public int NotificationID { get; set; } [Display(Name = "Comment")] public string description { get; set; } public int UserId { get; set; } public virtual UserProfile UserProfile { get; set; } public virtual ICollection<ReadNotif> ReadNotifs { get; set; } } public class ReadNotif { [Key] public int ReadNotifID { get; set; } public int userid { get; set; } public int NotificationID { get; set; } public virtual Notification Notification { get; set; } } 

Notifications and ReadNotifs tables are linked by the relationship One to many. So 1 Notification can be read (ReadNotif) several times.

I wish I could have this result :

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  | 
___________________________________ 

Remember : I would have to show data in a table "Notifications" based on the data in a table "ReadNotifs". So, i want to display a Notification when NotificationID(of Notifications) != NotificationID (ReadNotifs).

Any idea how I should proceed?

Thank you very much.

EDIT : Hi all. i tried something :

My ViewModel :

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

My Controller :

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

My View :

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

When i apply this method, let me show you the result with the last exemple :

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  |     ___________________________________
___________________________________ 

So this solution work partially.

Any idea ?

Ty

As I see from you code, the best approach for that is using "include" in your controller method, something like this:

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

To generate a collection of all Notifications except those that have a corresponding record in ReadNotifs (ie unread notifications), you can use the following query

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

Side note: Its not really clear what the purpose of the ReadNotif property is in your view model is (you never set it anywhere) so it seems unnecessary. You could just make the model in the view @model List<Notification>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM