C# 同一视图中的C2模型

C# 同一视图中的C2模型,c#,visual-studio,C#,Visual Studio,我想在同一页上查看两个视图。在我的HomeController中,我获取数据库的信息。但是当我想在我的视图中显示信息时,我遇到了问题 家庭控制器 public ActionResult DetailEquipe() { List<EquipePersonnel> Viewep = new List<EquipePersonnel>(); string path = HttpContext.Request.Url.Absolute

我想在同一页上查看两个视图。在我的HomeController中,我获取数据库的信息。但是当我想在我的视图中显示信息时,我遇到了问题

家庭控制器

 public ActionResult DetailEquipe()
    {
        List<EquipePersonnel> Viewep = new List<EquipePersonnel>();

        string path = HttpContext.Request.Url.AbsolutePath;
        //string id = path.Substring(path.LastIndexOf("/"));
        string id = System.IO.Path.GetFileName(path);
        ViewBag.ID = id;

        ViewBag.test = "testfail";
        List<Equipe> equipe = new List<Equipe>();
        List<Personnel> personnel = new List<Personnel>();
        try
        {
            using (var connection = new QC.SqlConnection(
         "Server = connection...."
         ))
            {
                connection.Open();
                Console.WriteLine("Connected successfully.");
                using (var command = new QC.SqlCommand())
                {
                    command.Connection = connection;
                    command.CommandType = DT.CommandType.Text;
                    command.CommandText = @"Select * from Equipe where Id='" + id + "'";
                    EquipePersonnel ep = new EquipePersonnel();
                    QC.SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        Equipe c = new Equipe();
                        c.equipe_id = reader["equipe_id"] as String;
                        c.Id = reader["Id"] as String;
                        c.equipe_nom_equipe = reader["equipe_nom_equipe"] as String;
                        equipe.Add(c);

                    }




                    command.CommandText = @"Select * from Personnel where Pers_ID_equipe='" + id + "'";

                    QC.SqlDataReader reader2 = command.ExecuteReader();

                    while (reader2.Read())
                    {
                        Personnel p = new Personnel();
                        p.Pers_Nom = reader2["Pers_Nom"] as String;
                        p.Pers_Prenom= reader2["Pers_Prenom"] as String;
                        p.Id= reader2["Id"] as String;
                        p.Pers_statut = reader2["Pers_statut"] as bool? ?? false;
                        personnel.Add(p);

                    }
                }
                connection.Close();
                    return View(Viewep);
                }
        }
        catch (Exception ex)
        {
            ViewBag.Message = "problème connection";
            Debug.WriteLine(ex.Message);

            return View(ex);
        }
    }
这是我的观点,我想看到设备和人员项目

@模型IList @{ Title=DétailEquipe; } @视图包测试 @foreach var项在Model.Equipe中 { 设备名称 }
创建视图模型,即HomeViewModel:

public class HomeViewModel 
{
    public List<Equipe> Equipe { get; set;}
    public List<Personnel> Personnel { get; set;}
}
然后将此模型返回到视图中

@model RapportDeChantier.Models.EquipePersonnel
@{
    ViewBag.Title = "DétailEquipe";
}
...
<div>
    <center>
        <h3>@ViewBag.test</h3>
        <table id="tableau_equippe">
           @foreach (var item in Model.Equipe)
            {
              <tr><td>Equipe</td><td>item.equipe_nom_equipe</td></tr>
            }
        </table>
   </center>
</div>

<div>
    <center>
        <h3>@ViewBag.test</h3>
        <table id="tableau_personnel">
           @foreach (var person in Model.Personnel)
            {
              <tr><td>Person</td><td>item.PersonNom</td></tr>
            }
        </table>
   </center>
</div>
可以像本例中那样使用Tuple

模型

public class Model1
{
  public string Test {get; set;}
}
public class Model2
{
  public string Test2 {get; set;}
}
控制器

 public ActionResult DetailEquipe()
 {
    Tuple<Model1, Model2> tuple = new Tuple<Model1, Model2>(new Model1(), new Model2());
    return View(tuple);
 }
.cshtml

@model Tuple<Model1,Model2>
<input asp-for="Item1.Test"/>   
<input asp-for="Item2.Test2"/>  

1.您的问题不是很清楚,但您的意思可能是希望在一个视图中有两个模型。在这种情况下,创建一个将其他两个模型作为属性的新模型,并传递该属性。2.控制器代码非常复杂,有些调用不是最佳实践,例如不使用块包装sql调用,甚至在控制器中直接使用sql调用,这不是很好的SoS。请阅读。确保你的问题是具体的,不要过于宽泛。这通常被称为Viewmodel。另外:参数化您的查询。想想小波比桌子!
@model Tuple<Model1,Model2>
<input asp-for="Item1.Test"/>   
<input asp-for="Item2.Test2"/>