c# 2 个模型在同一 View 中

标签 c# visual-studio

我想在同一个页面上有 2 个 View 。在我的 HomeController 中,我取回了我的数据库的信息。但是当我想在我的 View 中显示信息时,我遇到了问题。

家庭 Controller

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

这是我想看到装备和人员项目的 View

@model IList<RapportDeChantier.Models.EquipePersonnel>
@{
    ViewBag.Title = "DétailEquipe";
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <div>
        <center> <h3>@ViewBag.test</h3>
        <table id="tableau">
           @foreach (var item in Model.Equipe)
            {
          <tr><td>Equipe</td><td>item.equipe_nom_equipe</td></tr>
            }
        </table></center>
    </div>
</body>
</html>

最佳答案

创建 View 模型(即 HomeViewModel):

public class HomeViewModel 
{
    public List<Equipe> Equipe { get; set;}
    public List<Personnel> Personnel { get; set;}
}

从数据库中读取数据时填写EquipePersonnel:

var viewModel = new HomeViewModel();
viewModel.Equipe = //Equipe data
viewModel.Personnel = //Personnel data
return View(viewModel);

然后将此模型返回到您的 View 中。

@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>

关于c# 2 个模型在同一 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40848753/

相关文章:

c# - 当表为 varchar() 时,如何验证数据以防止注入(inject)攻击

c# - 什么是 System.Runtime.ActionItem?

visual-studio - 为什么我可以在没有管理员权限的情况下将 visual studio 附加到进程

python - Visual Studio fatal error C1510 : Cannot load language resource clui. dll。安装 Pandas 时

c++ - Visual C++ 调试器可视化工具?

c# - 数据层重构

c# - 无法添加服务引用

c# - 改变选择的RTF

visual-studio - 导出或复制 Visual Studio 6.0 设置

不同应用程序(WPF 和 Unity)中的 C# 结构序列化返回不同大小的字节数组