Royal Flash Royal Flash - 1 month ago 7
C# Question

Multiple tables with different LINQ queries in a single view

Title says it all, right now I have a single table that is populated from a linq query and I want to add more tables with different linq queries. I'm kinda lost on how would I do that..

I could probably do it if I create different views for each table but I want to have just one view for all. :D

Here's my code: (It's a table for "on going" projects)

Controller:

public ActionResult Index()
{

var project = from x in db.Projects
where x.Project_Status == "Ongoing"
select x;

return View(project);

}


Model:

public class Project
{
[Key]
public int Project_Id { get; set; }
public string Project_Name { get; set; }
public string Project_Detail { get; set; }
public string Project_Status { get; set; }
public int Employee_Id { get; set; }
}


View Model:

public class AdminHomeViewModel
{
public Project Ongoing { get; set; } //table for ongoing projects
public Project NYA { get; set; } //another table for Not Yet Assigned projects
public Employee Free { get; set; } //another table for free employees
public List<Project> OngoingList { get; set; }
public List<Employee> NYAList { get; set; }
public List<Employee> FreeList { get; set; }
}

Answer

You are confusing yourself with the different types of models. You should have a clear understanding between View-Model and Data-Model. You should always return View-Model to the View, and not the Data-Model. Data-Model are just the POCO classes which represents your data framework (in this case, each tables). There should be different Data Models for each of your table, which you must be having already based on your entity-framework approach (Code first, Model first or Database first). Then, prepare a single model for your view (as we can bind only one model to one view). Keep all the fields from different Data-Models that you need in that View and pass it along. See the approach below:

Data-Models

 public class Project
{
    [Key]
    public int Project_Id { get; set; }
    public string Project_Name { get; set; }
    public string Project_Detail { get; set; }
    public string Project_Status { get; set; }
    public int Employee_Id { get; set; }
}

 public class Employee
{
    [Key]
    public int Employee_Id { get; set; }
    public string Employee_Name { get; set; }
    public string Employee_Detail { get; set; }   
}

View-Model

public class MyViewModel
{
    public int Project_Id { get; set; }
    public string Project_Name { get; set; }
    public string Project_Detail { get; set; }
    public string Project_Status { get; set; }
    public int Employee_Id { get; set; }
    public string Employee_Name { get; set; }
    public string Employee_Detail { get; set; }   
}

Or

public class MyViewModel
{
   public Project proj { get; set; }  
   public Employee emp { get; set; }  
}

Pass it to view as:

public ActionResult Index()
    { 

      MyViewModel model = new MyViewModel();

      // You linq query to populate model goes here

       return View(model);

    }

Update:

From my understanding, you need something like this:

View-Model:

public class AdminHomeViewModel
{
    public AdminHomeViewModel()
    {
        Ongoing = new List<Project>();
        NYA = new List<Project>();
        Free = new List<Employee>();
    }

    public List<Project> Ongoing { get; set; } //table for ongoing projects
    public List<Project> NYA { get; set; } //another table for Not Yet Assigned projects
    public List<Employee> Free { get; set; } //another table for free employees
}

Controller:

   public ActionResult Index()
    { 

    AdminHomeViewModel model = new AdminHomeViewModel();

      var result1 = (from x in db.Projects
                          where x.Project_Status == "Ongoing"
                          select new Project(){
                              Project_Id = x.Project_Id ,
                              Project_Name = x.Project_Name,
                              ... //all other assignments goes here
                          }).ToList();

var result2 = (from x in db.Projects
                          where x.Project_Status == "blah blah"
                          select new Project(){
                              Project_Id = x.Project_Id ,
                              Project_Name = x.Project_Name,
                              ... //all other assignments goes here
                          }).ToList();

var result3 = (from x in db.Employee
                          where x.AnyCondition == "blah blah"
                          select new Employee(){
                              Employee_Id = x.Employee_Id ,
                              Employee_Name = x.Employee_Name,
                              ... //all other assignments goes here
                          }).ToList();

    model.Ongoing = result1;
    model.NYA  = result2;
    model.Free = result3;


    return View(model);


    }
Comments