[英]how to display multiple joined models at asp.net mvc one view
I have three models employee, permission, and division.我有三个模型员工、权限和部门。 I made LINQ join between them.我让 LINQ 在它们之间加入。
How can I make a strongly typed view out of three joined models and how to show them all in one table?如何从三个连接模型中创建强类型视图,以及如何将它们全部显示在一个表中?
the models are formed like this.模型是这样形成的。
employee员工
public class Employee : IdentityUser
{
[Key]
public override string Id { get => base.Id; set => base.Id = value; }
public override string Email { get => base.Email; set => base.Email = value; }
public string HomePhone { get; set; }
public string OfficePhone { get; set; }
public string EmpName { get; set; }
public int? DivID { get; set; }
[ForeignKey("DivID")]
public Division division { get; set; }
}
permission允许
public class Permission
{
[Key]
public int PerID { get; set; }
public string PermissionsList { get; set; }
public string BlockList { get; set; }
public int DivID { get; set; }
public string EmpID { get; set; }
public Employee employee { get; set; }
}
division分配
public class Division
{
[Key]
public int DivID { get; set; }
public string DivName { get; set; }
public List<Employee> employees { get; set; }
}
LINQ LINQ
var GetAll = from E in GetEmployeeList
join P in GetpermissionList
on E.Id equals P.EmpID
join D in GetdivisionList
on E.DivID equals D.DivID
select new { Employees = E, Permissions = P, Divisions = D };
this is how GetAll looks like from inside这就是 GetAll 从内部看的样子
Now, the question again.现在,问题又来了。
how to display a combination of three models "GetAll" in a table in one view altogether?如何在一个视图中的表格中显示三个模型“GetAll”的组合? but also I need to know, how can I make my view strongly typed in case of such a complicated model?但我还需要知道,在如此复杂的 model 的情况下,如何使我的视图成为强类型?
for example, not a solution, I used this to make the view strongly typed, but it is the wrong dummy try just to approach what I mean例如,不是一个解决方案,我用它来使视图强类型化,但这是错误的虚拟尝试只是接近我的意思
@model IEnumerable<Employees, Permissions, Divisions>
// In the controller -----> return View(GetAll);
Simply you create a new class (View model) that has three properties, one for each entity.您只需创建一个新的 class(查看模型),它具有三个属性,每个实体一个。 Then, change the last line in your linq to select new MyViewModel { Employee = E, Permission = P, Division = D }然后,将 linq 中的最后一行更改为 select new MyViewModel { Employee = E, Permission = P, Division = D }
You can create view model class such as;您可以创建视图 model class 等;
public class ViewModel
{
public Employees Employees {get;set;}
public Permissions Permissions { get; set; }
public Divisions Divisions { get; set; }
}
then the query should be;那么查询应该是;
var GetAll = from E in GetEmployeeList
join P in GetpermissionList
on E.Id equals P.EmpID
join D in GetdivisionList
on E.DivID equals D.DivID
select new ViewModel { Employees = E, Permissions = P, Divisions = D };
You can pass the list of view model to view,可以通过查看列表model来查看,
@model List<ViewModel >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.