Finally got a solution for my problem ...
This is the ViewModel I created ...
public class UserRoleViewModel
{
public string fname { get; set; }
public string rname { get; set; }
}
This is the controller ...
public ActionResult Users()
{
var result = from user in db.Users
from role in db.Roles
where role.Users.Any(r => r.UserId == user.Id)
select new UserRoleViewModel
{
fname = user.FirstName,
rname = role.Name
};
return View(result);
}
And this is the view ...
@model IEnumerable<ApplicationName.Models.UserRoleViewModel>
<ul>
@foreach(var u in Model)
{
<li>@u.fname - @u.rname</li>
}
</ul>
I'm still new to LINQ, Lambda, and MVC in general. If anyone has a way to improve this code, feel free to add your opinions.
source
share