[英]How to display all rows with same ID in ASP.NET MVC?
My Index page displays all rows from an Azure SQL Database. 我的索引页面显示Azure SQL数据库中的所有行。 When I click a row I get redirected to a unique URL with a QR code for that URL and it also displays the data for only that row.
当我单击一行时,我被重定向到具有该URL的QR码的唯一URL,并且它还仅显示该行的数据。 Each row has it's own unique ID, but also a truckID.
每行都有自己的唯一ID,但也有truckID。 The truckID is for a driver who can "own" several rows of data, so I want to show every row with the same truckID after a row is clicked.
truckID适用于可以“拥有”几行数据的驾驶员,因此我想在单击一行后显示具有相同truckID的每一行。
This is what I've got so far: 到目前为止,这是我得到的:
MODEL 模型
namespace QR
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("data")]
public partial class data
{
[Required]
[StringLength(50)]
public string Contract { get; set; }
[StringLength(50)]
public string Sort { get; set; }
public int? Report { get; set; }
public double? InputKG { get; set; }
public double? OutputKG { get; set; }
public double? Recovery { get; set; }
public double? Si { get; set; }
public double? Cu { get; set; }
public double? Mn { get; set; }
public double? Mg { get; set; }
public double? Zn { get; set; }
public double? Cr { get; set; }
public double? Fe { get; set; }
public double? Pb { get; set; }
public int? truckID { get; set; }
[Key]
public int ID{ get; set; }
}
}
VIEW (after a row is clicked from Index) VIEW(从索引中单击一行后)
@model QR.data
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>QR code</h2>
<img src="/Home/BarcodeImage?barcodeText=@Request.Url.OriginalString"/>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Contract)
</th>
<th>
@Html.DisplayNameFor(model => model.Sort)
</th>
<th>
@Html.DisplayNameFor(model => model.Report)
</th>
<th>
@Html.DisplayNameFor(model => model.InputKG)
</th>
<th>
@Html.DisplayNameFor(model => model.OutputKG)
</th>
<th>
@Html.DisplayNameFor(model => model.Recovery)
</th>
<th>
@Html.DisplayNameFor(model => model.Si)
</th>
<th>
@Html.DisplayNameFor(model => model.Cu)
</th>
<th>
@Html.DisplayNameFor(model => model.Mn)
</th>
<th>
@Html.DisplayNameFor(model => model.Mg)
</th>
<th>
@Html.DisplayNameFor(model => model.Zn)
</th>
<th>
@Html.DisplayNameFor(model => model.Cr)
</th>
<th>
@Html.DisplayNameFor(model => model.Fe)
</th>
<th>
@Html.DisplayNameFor(model => model.Pb)
</th>
</tr>
<tr>
<td>
@Html.DisplayFor(model => model.Contract)
</td>
<td>
@Html.DisplayFor(model => model.Sort)
</td>
<td>
@Html.DisplayFor(model => model.Report)
</td>
<td>
@Html.DisplayFor(model => model.InputKG)
</td>
<td>
@Html.DisplayFor(model => model.OutputKG)
</td>
<td>
@Html.DisplayFor(model => model.Recovery)
</td>
<td>
@Html.DisplayFor(model => model.Si)
</td>
<td>
@Html.DisplayFor(model => model.Cu)
</td>
<td>
@Html.DisplayFor(model => model.Mn)
</td>
<td>
@Html.DisplayFor(model => model.Mg)
</td>
<td>
@Html.DisplayFor(model => model.Zn)
</td>
<td>
@Html.DisplayFor(model => model.Cr)
</td>
<td>
@Html.DisplayFor(model => model.Fe)
</td>
<td>
@Html.DisplayFor(model => model.Pb)
</td>
</tr>
</table>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
CONTROLLER 控制器
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
data data= db.data.Find(id);
if (data == null)
{
return HttpNotFound();
}
return View(data);
}
And on the Index I have this link: 在索引上,我有此链接:
@Html.ActionLink("QR", "Details", new { id = item.ID})
Help? 救命?
With this answer I presume two things: 有了这个答案,我假设有两件事:
db
is a DbContext
and has a property called data
which is a DbSet<data>
db
是DbContext
并且具有一个名为data
的属性,该属性是DbSet<data>
data
class data
类 If the above is true, here's what you should do: 如果以上是正确的,那么您应该执行以下操作:
In the controller: 在控制器中:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
data data = db.data.Find(id);
if (data == null)
{
return HttpNotFound();
}
var groupedData = db.data.Where(d => d.truckID == data.truckID).ToList();
return View(groupedData);
}
In your view (Details) 在您看来(详细信息)
// This is after declaring the table headers
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(model => item.Contract)
</td>
// Fill in rest of the td rows as you did
<td>
@Html.DisplayFor(model => item.Pb)
</td>
</tr>
}
This should display all of the rows correctly. 这应该正确显示所有行。
Also, this is just for readability, but it is really important, capitalize your class names , ie it should be public partial class Data
not data
. 同样,这只是为了提高可读性,但将类名大写是非常重要的,即它应该是
public partial class Data
而不是data
。
You didn't get any error on this statement ? 您没有对这条语句有任何错误?
data data= db.data.Find(id);
I think it has to be like this 我认为一定是这样
data data= db.data.Find(i=>i.ID == id);
and I supposed you were using foreach loop to print all of the data and in that loop you have that @Html.ActionLink. 我以为您正在使用foreach循环来打印所有数据,并且在该循环中您拥有了@ Html.ActionLink。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.