Buddy ViewModel with LINQ Join

2019-06-13 07:59发布

问题:

SOS! What I am trying to achieve is for the logged in person, to see the users he or she is supporting (buddying). I am now trying to fully embrace ViewModels to coagulate views. I am using simplemembership MVC4 with Mysql. I have a UserProperties(all details of my users)linked to Userprofile and Everything else works. I usually use two databases one for membership and another for all other stuff.

models

UserProfile/UserProPerties - extended for all other properties

  • UserId
  • List item
  • UserName

UserProperty

  • FirstName
  • LastName
  • SchoolName
  • UserId

Buddyship

  • buddyId
  • buddiedByUserId
  • buddiedUserId

Viewmodel model

 public class BuddyViewModel
  {
      public BuddyShip BuddyShip {get; set;}
      public List<Buddyship> AllBudees {get; set;}
      public UserProperty UserProperty { get; set; }
      public PanelViewModel(Buddyship buddyship, List<Buddyship> allBudees)
      {
            Buddyship = buddyship;
           AllBudees = allBudees;
      }
  }

BuddyViewModel Controller

   // I  believe this is where the magic should come from 
   public ActionResult Index(int? id)
   {
         //I get logged in user properties
        var user = db.UserProperties.SingleOrDefault(x => x.UserName == User.Identity.Name);            
        Buddyship allBudees = db1.Buddyships.SingleOrDefault(u =>u.BuddiedByUserId == user.UserId);
        var buds = from u in db.UserProperties
                   join m in db1.Buddyships on u.UserId equals m.BuddiedByUserId
                   where m.BuddiedByUserId == user.UserId
                   select new { u.FirstName, u.LastName, u.SchoolName, u.UserId };

        var buddyviewmodel = new BuddyViewModel(buds //don't know what to put here);

        return View(buddyviewmodel);
    }

View

      @model IEnumerable<BudTT.Models.BuddyViewModel>
      @foreach (var item in Model.Buddyships) 
      {
        <p>@Html.DisplayFor(model =>model.UserProperty.FirstName)</p>
        <p>@Html.DisplayFor(model =>model.UserProperty.LastName)</p>

      }

Thanks if you are able to help

回答1:

Try to change your code as follows.

ViewModel:

public class BuddyViewModel
  {
      public BuddyShip BuddyShip {get; set;}
      public List<Buddyship> AllBudees {get; set;}
      public List<UserProperty> Users { get; set; } //** Add this property
   }

Your action:

    public ActionResult Index(int? id)
       {
             //I get logged in user properties
            var user = db.UserProperties.SingleOrDefault(x => x.UserName == User.Identity.Name);  

///**************Get Buddyships of current user*************************          
            List<Buddyship> allBudees = db1.Buddyships.Where(u =>u.BuddiedByUserId == user.UserId).ToList();

///**************Get Users supporting by user*************************  
            var buds = 
                    (from u in db.UserProperties
                     join m in allBudees on u.UserId equals m.buddiedUserId
                     where m.BuddiedByUserId == user.UserId
                     select new UserProperty
                     {
                      FirstName = u.FirstName, 
                      LastName = u.LastName, 
                      SchoolName = u.SchoolName, 
                      UserId = u.UserId 
                      }).ToList();

            var buddyviewmodel = new BuddyViewModel
            {
              Users = buds,
              AllBudees = allBudees, //** if you really need this property in your View,
              ...
              }

            return View(buddyviewmodel);
        }

Change View. In your action you are sending only one BuddyViewModel, which contains a list of UserProperties, but not a list of BuddyViewModels

@model BudTT.Models.BuddyViewModel
      @foreach (var item in Model.Users) 
      {
        <p>@item.FirstName</p>
        <p>@item.LastName</p>
      }