Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to access two model records in one model.

I configured my database and table with entity framework using database schema based approach.

I mean,
table 1 called dept and table 2 called Employee

so, dbcontext creates classes like follow right

public class dept{
public int dept_id{get;set;}
public string dept_name{get;set;}
}

public class Employee{
public int emp_id{get;set;}
public string emp_name{get;set;}
}

Now in the Homecotroller, if i want to use dept records only, i can use using linq query and return to the list
something like follow,

public class DefaultController : Controller
{

WFTOOLSDBContext3 dbcontext = new WFTOOLSDBContext3();
public ActionResult Index()
{
var ivtflist = (from a in dbcontext.depts
select new dept
{
dept_id = a.id,
dept_name= a.dept_name
}).Take(16);
}
}

in view i can put,

@model IEnumerable<dropshiptest.dept> then i can use records using foreach however i need.


But i want to take records the following column from both model (dept as well as employee)
dept_id,
dept_name,
and emp_name

so i tried viewmodel concept follow

step 1: i created another another class called ViewModelDemoVM.cs

ie,

public class ViewModelDemoVM
{
public List<dept> getdept { get; set; }
public List<employee> getemployee { get; set; }

}

setp 2: then in action method of index i tried LINQ query using join follow

var getbothrecord = (from item in dbcontext.depts

join emp in dbcontext.Employee
on item.dept_id equals emp.emp_id
select new ViewModelDemoVM
{
// Here i got stuck , i am getting object of ViewModelDemoVM class, But do not how to do and what to do
//how to access two models records?

}).ToList();

return View(getbothrecord);

how to get two models records in one model and return to the view ?
Posted

1.Your ViewModelDemoVM class have to contain only the needed properties from both tables and nothing more. So it should be like:
C#
public class ViewModelDemoVM
{
 public int DeptID { get; set; }
 public string DeptName { get; set; }
 public string EmpName{ get;set;}
}

2.You should do similar like in your first LINQ, but in this case to use the properties from your new class like below:
C#
var getbothrecord = (from item in dbcontext.depts as a
join emp in dbcontext.Employee as b
on item.dept_id equals emp.emp_id
select new ViewModelDemoVM
{
 DeptID = a.id;
 DeptName = a.dept_name;
 EmpName = b.emp_name; 
}).ToList();
 
Share this answer
 
Create new class that will take two references to particular employee and department. Use it as a model.

OR

Change your Emp model to have a join to Dept so you could join them
This part seems incorrect unless you have only one employee per department.

item.dept_id equals emp.emp_id
 
Share this answer
 
i think this will help you

when you want to use two models in a single view then first you create a new model
class dept
{
public long id{get;set}
public string name{get;set}
}
class emp
{
public long id{get;set}
public string name{get;set}
}


class MixModel
{
//now create properties
public List<dept> deptlist {get;set;}
public List<emp> emplist {get;set;}
}

// now use Mix model to assign values and pass Mixmodel object to view :-)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900