2017-08-25 60 views
0

我的索引頁面顯示來自Azure SQL數據庫的所有行。當我點擊一行時,我會被重定向到一個具有該URL的QR碼的唯一URL,並且它也會顯示該行的數據。每行都有它自己的唯一ID,但也有一個卡車ID。卡車ID是可以「擁有」多行數據的驅動程序,所以我想在單擊行後顯示具有相同卡車ID的每一行。如何在ASP.NET MVC中顯示具有相同ID的所有行?

這是我到目前爲止有:

模型

 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(行從指數點擊後)

 @model QR.data 

     @{ 
      ViewBag.Title = "Details"; 
      Layout = "~/Views/Shared/_Layout.cshtml"; 
     } 

     <h2>QR code</h2> 
      <img src="/Home/[email protected]"/> 

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

控制器

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

而在索引上我有這個鏈接:

 @Html.ActionLink("QR", "Details", new { id = item.ID}) 

幫助?

+0

我看不出你是如何將你的TruckID整合到任何地方的數據/視圖中的......你的查詢可能類似於'data data = db.data.Where(x => x.TruckID = YourTruckIDVariable ).ToList()',但您的視圖也需要一些工作以及... – jleach

回答

-1

您在此聲明中沒有收到任何錯誤?

data data= db.data.Find(id); 

我認爲它是這樣

data data= db.data.Find(i=>i.ID == id); 

,我以爲你是使用foreach循環打印所有數據,並在循環中,您有@ Html.ActionLink。

+0

沒有錯誤。是的,索引上的ActionLink有一個foreach循環'@foreach(Model中的var項)' – morkeseth

1

這個答案我相信兩件事情:

  • db是一個DbContext並有一個名爲data屬性,它是一個DbSet<data>
  • 您沒有任何其他型號爲您的數據,只有data

如果以上是真的,這裏是你應該做的:

在控制器:

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

在您的視圖(詳細信息)

// 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> 
} 

應該顯示所有正確的行。

而且,這僅僅是可讀性,但它是非常重要的,利用你的類名,即它應該是public partial class Datadata

+0

您的推測是正確的,但代碼在Details頁面上顯示一個空表。你有什麼想法,爲什麼?此外,Details頁面應該如何知道要顯示的truckID行? – morkeseth

+0

以下是我的想法:索引頁顯示記錄 - 由'id'標識。有幾行具有不同'id'-s但相同的'truckID'-s。我的解決方案找到具有選定「id」的行,然後使用該記錄的「truckID」來收集其餘數據。我意識到,我錯過了'db.data.Where(d => d.truckID == data.truckID)'末尾的'.ToList()''。添加,或只是複製我更新的答案,行應顯示。 –

+0

正確,索引每行顯示一個唯一的ID。每一行也有一個卡車ID,但多行可以有相同的卡車ID。但是在實現你的代碼後,視圖仍然是空的。有沒有其他的代碼可能需要一個片段? – morkeseth

相關問題