[英]ASP.net MVC5 Showing data from a joined ViewModel
I have this ViewModel here called UsersViewModel. 我这里有一个名为UsersViewModel的ViewModel。 It is joined with a table called userextended. 它与一个名为userextended的表结合在一起。
public class UsersViewModel
{
public ApplicationUser aspNetUsers { get; set; }
public AspNetUsersExtended aspUsersExtended { get; set; }
}
I am stuck on what I should do that join the tables and push them out to the view. 我坚持将表加入表中并将其推出视图。 This is my controller: 这是我的控制器:
private ApplicationDbContext dbContext = new ApplicationDbContext();
// GET: Users
public ActionResult Index()
{
return View();
}
private List<UsersViewModel> GetUsers()
{
var userStore = new UserStore<ApplicationUser>(dbContext);
List<UsersViewModel> uvml = new List<UsersViewModel>();
AspNetUsersExtended[] usersExtended = dbContext.AspNetUsersExtended.ToArray();
return uvml;
}
I've seen some examples where they do sql query joins but I'm wondering if the viewmodel can already join it automatically like in the model? 我已经看过一些示例,它们在其中执行sql查询联接,但是我想知道viewmodel是否可以像模型中那样自动联接它?
UPDATED: 更新:
@model IEnumerable<ManagementStudio.ViewModels.UsersViewModel>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Users</h2>
@foreach (var item in Model)
{
<p>@item.aspNetUsers.Id</p>
<p>@item.aspNetUsers.Email</p>
<p>@item.aspUsersExtended.Notes</p>
}
Assuming there's relation between ApplicationUser and AspNetUsersExtended, you can use linq2sql way to join both collection like so 假设ApplicationUser和AspNetUsersExtended之间存在关系,则可以使用linq2sql方式像这样加入两个集合
var userStore = new UserStore<ApplicationUser>(dbContext);
List<UsersViewModel> uvml = (from user in userStore.Users
join ext in dbContext.AspNetUsersExtended
on user.X equals ext.X
select new UsersViewModel
{
aspNetUsers = user,
aspUsersExtended = ext
}).ToList();
return uvml;
Didn't have machine with me to test it out at the moment, hope it helps. 目前没有机器可以测试,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.