0

求救!我想要实现的是让登录的人看到他或她支持的用户(好友)。我现在正尝试完全接受 ViewModels 来凝聚视图。我正在将 simplemembership MVC4 与 Mysql 一起使用。我有一个链接到 Userprofile 的 UserProperties(我的用户的所有详细信息),其他一切都有效。我通常使用两个数据库,一个用于会员资格,另一个用于所有其他内容。

楷模

UserProfile/UserProPerties - 扩展所有其他属性

  • 用户身份
  • 项目清单
  • 用户名

用户属性

  • 学校名称
  • 用户身份

伙伴关系

  • 好友ID
  • buddiedByUserId
  • 好友用户ID

视图模型模型

 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 控制器

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

看法

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

      }

谢谢如果你能帮忙

4

1 回答 1

2

尝试如下更改您的代码。

视图模型:

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

你的行动:

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

更改视图。在您的操作中,您只发送一个 BuddyViewModel,其中包含 UserProperties 列表,但不包含 BuddyViewModels 列表

@model BudTT.Models.BuddyViewModel
      @foreach (var item in Model.Users) 
      {
        <p>@item.FirstName</p>
        <p>@item.LastName</p>
      }
于 2013-08-19T20:28:11.947 回答