我想在同一页面中拍摄2个视图。在我的HomeController中,我得到了我的数据库的信息。但是当我想在我的观点中显示信息时,我遇到了问题。
Homecontroler
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);
}
}
我想看看Equipe和Personnel项目
的观点
@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>
&#13;
答案 0 :(得分:7)
创建视图模型(即HomeViewModel):
public class HomeViewModel
{
public List<Equipe> Equipe { get; set;}
public List<Personnel> Personnel { get; set;}
}
从数据库中读取数据时填写Equipe
和Personnel
:
var viewModel = new HomeViewModel();
viewModel.Equipe = //Equipe data
viewModel.Personnel = //Personnel data
return View(viewModel);
然后将此模型返回到您的视图中。
@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>
答案 1 :(得分:0)
您可以在此示例中使用Tuple<Model1,Model2>
模型
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"/>