简体   繁体   English

在单个视图中具有不同LINQ查询的多个表

[英]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. 标题说明了一切,现在我有一个从linq查询填充的表,并且我想添加更多具有不同linq查询的表。 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 :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; }    
}

You are confusing yourself with the different types of models. 您将自己与不同类型的模型混淆了。 You should have a clear understanding between View-Model and Data-Model. 您应该对View-Model和Data-Model有清楚的了解。 You should always return View-Model to the View, and not the Data-Model. 您应该始终将View-Model而不是Data-Model返回到View。 Data-Model are just the POCO classes which represents your data framework (in this case, each tables). 数据模型只是代表您的数据框架(在本例中为每个表)的POCO类。 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);


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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