You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to display two class result in to view using LINQ?

    Here is the controller action method code but I don't have any Idea that How can I display that result to view..??

    public class DetailsController : Controller
    {
    //
    // GET: /Details/


    List<Employee> employees = new List<Employee>();
    List<Department> departments = new List<Department>();

    public ActionResult Index()
    {
    employees.Add(new Employee { DepartmentID = 1, EmployeeID = 1, EmployeeName = "Kailas" });
    employees.Add(new Employee { DepartmentID = 2, EmployeeID = 2, EmployeeName = "Dipak" });
    employees.Add(new Employee { DepartmentID = 2, EmployeeID = 3, EmployeeName = "Pramod" });
    employees.Add(new Employee { DepartmentID = 1, EmployeeID = 4, EmployeeName = "Prakash" });
    employees.Add(new Employee { DepartmentID = 3, EmployeeID = 5, EmployeeName = "Pranav" });
    employees.Add(new Employee { DepartmentID = 4, EmployeeID = 6, EmployeeName = "Dipesh" });

    departments.Add(new Department { DepartmentID = 1, DepartmentName = "IT" });
    departments.Add(new Department { DepartmentID = 1, DepartmentName = "BA" });
    departments.Add(new Department { DepartmentID = 1, DepartmentName = "CS" });
    departments.Add(new Department { DepartmentID = 1, DepartmentName = "MBA" });
    departments.Add(new Department { DepartmentID = 1, DepartmentName = "Comm" });

    var list = (from e in employees
    join d in departments
    on e.DepartmentID equals d.DepartmentID
    select new {
    EmployeeName = e.EmployeeName,
    DepartmentName = d.DepartmentName
    }).ToList();

    return View(list);
    }
    }
  • #761339
    You can achive this by two ways
    1. You can use ViewBag. In the viewbag you can add this two model - (But this is not good practice)
    2. You can create one model like ViewModel (EmpDept). Add add the two models(Emp and Dep) into it. Return "EmpDept". you can access those two models there..

    By Nathan
    Direction is important than speed


Sign In to post your comments