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

C# 在单个视图中具有不同LINQ查询的多个表,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,标题说明了一切,现在我有一个从linq查询填充的表,我想添加更多具有不同linq查询的表。我有点不知道该怎么做 如果我为每个表创建不同的视图,我可能会这样做,但我希望所有表都只有一个视图:D 这是我的代码:(这是一个“正在进行的”项目的表格) 控制器: public ActionResult Index() { var project = from x in db.Projects where x.Proje

标题说明了一切,现在我有一个从linq查询填充的表,我想添加更多具有不同linq查询的表。我有点不知道该怎么做

如果我为每个表创建不同的视图,我可能会这样做,但我希望所有表都只有一个视图:D

这是我的代码:(这是一个“正在进行的”项目的表格)

控制器:

public ActionResult Index()
    { 

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

            return View(project);

    }
 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 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; }    
}
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
}
   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);


    }
型号:

public ActionResult Index()
    { 

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

            return View(project);

    }
 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 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; }    
}
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
}
   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);


    }
查看模型:

public ActionResult Index()
    { 

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

            return View(project);

    }
 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 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; }    
}
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
}
   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);


    }
公共类AdminHomeViewModel
{
正在进行的公共项目{get;set;}//正在进行的项目的表
公共项目NYA{get;set;}//尚未分配项目的另一个表
public Employee Free{get;set;}//另一个表,用于自由雇员
公共列表OngoingList{get;set;}
公共列表列表{get;set;}
公共列表自由列表{get;set;}
}

您对不同类型的型号感到困惑。您应该清楚地了解视图模型和数据模型。您应该始终将视图模型返回到视图,而不是数据模型。数据模型只是表示数据框架(在本例中为每个表)的POCO类。每个表都应该有不同的数据模型,根据实体框架方法(代码优先、模型优先或数据库优先),这些数据模型必须已经存在。然后,为您的视图准备一个模型(因为我们只能将一个模型绑定到一个视图)。保留该视图中所需的不同数据模型中的所有字段,并将其传递。见下面的方法:

数据模型

 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; }   
}
查看模型

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; }   
}

public class MyViewModel
{
   public Project proj { get; set; }  
   public Employee emp { get; set; }  
}
将其作为以下内容传递给视图:

public ActionResult Index()
    { 

      MyViewModel model = new MyViewModel();

      // You linq query to populate model goes here

       return View(model);

    }
更新:

public ActionResult Index()
    { 

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

            return View(project);

    }
 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 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; }    
}
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
}
   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);


    }
根据我的理解,你需要这样的东西:

查看模型:

public ActionResult Index()
    { 

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

            return View(project);

    }
 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 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; }    
}
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
}
   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);


    }

谢谢实际上我就是这么做的。。现在,我只需要弄清楚如何将linq的结果传递给视图模型。您的linq查询和视图模型类是什么?我可以帮助您将linq结果填充到视图模型中。我的linq查询与此相同,我将在第一篇文章中添加视图模型。您的linq查询将返回一条记录还是多条记录?在多个记录的情况下,您可能需要考虑在VIEW模型中添加列表、列表和列表。多个记录,好的,我会。这是否意味着我不需要公共项目正在进行,而应该将其更改为公共列表正在进行?为什么不使用局部视图?