巴迪视图模型与LINQ加入 [英] Buddy ViewModel with LINQ Join
问题描述
SOS!我试图做到的,是为亲自记录下来,看看用户,他或她正在支持(伙伴系统)。我现在想完全接受的ViewModels凝结意见。我使用simplemembership MVC4与MySQL。我有一个链接到一个USERPROFILE UserProperties(我的用户的所有细节)和其他一切工作。我通常使用两个数据库之一的成员,另一个用于所有其他的东西。
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.
模式
的用户配置/ UserProPerties 的 - 扩展到所有其他属性。
UserProfile/UserProPerties - extended for all other properties
- 用户ID
- 项目
- 用户名
的 UserProperty 的
- 姓
- 姓
- SchoolName
- 用户ID
的 Buddyship 的
- buddyId
- buddiedByUserId
- buddiedUserId
视图模型模型
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控制器
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);
}
查看
@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
推荐答案
试着改变你的code如下:
Try to change your code as follows.
视图模型:
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名单
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>
}
这篇关于巴迪视图模型与LINQ加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!