求救!我想要实现的是让登录的人看到他或她支持的用户(好友)。我现在正尝试完全接受 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>
}
谢谢如果你能帮忙