如何在 asp.net table 中显示来自查询的数据
How to show data from query in table on asp.net
var query = (from p in db.Produits
join v in db.Vondus on p.ProduitId equals v.ProduitId
where p.CentreId == centre.CentreId
select new
{
nom = p.ProduitNom,
date = v.VonduDate,
prix = p.ProduitPrix
}).ToList();
我想在 html table 中显示此查询。
你能帮帮我吗
最简单的方法是将结果分配到模型,然后将其发送到查看.
型号
namespace DemoMvc.Models
{
public class ProduitModel
{
public string ProduitNom { get; set; }
public DateTime VonduDate { get; set; }
public string ProduitPrix { get; set; }
}
}
操作方法
public ActionResult Index()
{
var result = (from p in db.Produits
join v in db.Vondus on p.ProduitId equals v.ProduitId
where p.CentreId == centre.CentreId
select new ProduitModel
{
nom = p.ProduitNom,
date = v.VonduDate,
prix = p.ProduitPrix
}).ToList();
return View(result);
}
查看
确保模型的命名空间与实际命名空间匹配。
@model IEnumerable<DemoMvc.Models.ProduitModel>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<table class="table table-bordered">
<tr>
<th>ProduitNom</th>
<th>VonduDate</th>
<th>ProduitPrix</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.ProduitNom</td>
<td>@item.VonduDate.ToShortDateString()</td>
<td>@item.ProduitPrix</td>
</tr>
}
</table>
</body>
</html>
结果
var query = (from p in db.Produits
join v in db.Vondus on p.ProduitId equals v.ProduitId
where p.CentreId == centre.CentreId
select new
{
nom = p.ProduitNom,
date = v.VonduDate,
prix = p.ProduitPrix
}).ToList();
我想在 html table 中显示此查询。 你能帮帮我吗
最简单的方法是将结果分配到模型,然后将其发送到查看.
型号
namespace DemoMvc.Models
{
public class ProduitModel
{
public string ProduitNom { get; set; }
public DateTime VonduDate { get; set; }
public string ProduitPrix { get; set; }
}
}
操作方法
public ActionResult Index()
{
var result = (from p in db.Produits
join v in db.Vondus on p.ProduitId equals v.ProduitId
where p.CentreId == centre.CentreId
select new ProduitModel
{
nom = p.ProduitNom,
date = v.VonduDate,
prix = p.ProduitPrix
}).ToList();
return View(result);
}
查看
确保模型的命名空间与实际命名空间匹配。
@model IEnumerable<DemoMvc.Models.ProduitModel>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<table class="table table-bordered">
<tr>
<th>ProduitNom</th>
<th>VonduDate</th>
<th>ProduitPrix</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.ProduitNom</td>
<td>@item.VonduDate.ToShortDateString()</td>
<td>@item.ProduitPrix</td>
</tr>
}
</table>
</body>
</html>