简体   繁体   中英

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.

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

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

Now, the question again.
how to display a combination of three models "GetAll" in a table in one view altogether? but also I need to know, how can I make my view strongly typed in case of such a complicated 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. Then, change the last line in your linq to select new MyViewModel { Employee = E, Permission = P, Division = D }

You can create view model class such as;

 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 List<ViewModel > 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM