I have this ViewModel here called UsersViewModel. It is joined with a table called 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?
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.