简体   繁体   English

如何在ASP.NET MVC中显示具有相同ID的所有行?

[英]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> dbDbContext并且具有一个名为data的属性,该属性是DbSet<data>
  • You don't have any other models for your data, only the 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.

相关问题 如何在 ASP.NET MVC(多对多)中基于 Book 的 ID 显示所有作者 - How to display all Authors based on ID of Book in ASP.NET MVC (many-to-many) 如何在asp.net mvc 4中上传图像并显示在同一页面上 - How to upload a image and display on same page in asp.net mvc 4 如何在所有内容页面上显示图像ASP.Net MVC - How to display image on all content pages ASP.Net MVC ASP.NET MVC:如何通过一个控制器操作同时打开具有相同特定ID的所有文档(例如pdf)? - ASP.NET MVC: How can I open simultaneously all documents (say pdf's) , with same particular ID with one controller action? 在表中保存记录后,如何循环遍历具有相同 ID 的所有记录并在 ASP.NET MVC C# 中更新它们 - After saving a record in a table, how to loop through all records with same ID and update them in ASP.NET MVC C# 处理结果并显示在同一页面的asp.net mvc - process result and display in same page asp.net mvc 在同一页面上显示来自 ASP.NET MVC 5 中 2 个表的数据 - Display data from 2 tables in ASP.NET MVC 5 on the same page 如何使用asp.net mvc 5从Excel工作表显示10个第一行 - how to display 10 first rows from excel sheet with asp.net mvc 5 获取与ID相关的所有值(ASP.NET MVC) - Get all values related to id (ASP.NET MVC) 如何在 ASP.NET MVC 的录取表格成功页面上显示来自 model 的 id? - How to display id from model on a admission form success page in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM