【问题标题】:How to display all rows with same ID in ASP.NET MVC?如何在 ASP.NET MVC 中显示具有相同 ID 的所有行?
【发布时间】:2017-08-25 11:09:19
【问题描述】:

我的索引页面显示 Azure SQL 数据库中的所有行。当我单击一行时,我会被重定向到一个带有该 URL 二维码的唯一 URL,并且它还仅显示该行的数据。每行都有自己的唯一 ID,但也有一个卡车 ID。 TruckID 适用于可以“拥有”多行数据的驾驶员,因此我希望在单击一行后显示每一行都具有相同的 truckID。

这是我目前所得到的:

型号

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

            }
        }

查看(从索引中单击一行后)

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

控制器

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

帮助?

【问题讨论】:

  • 我看不出您是如何尝试将您的 TruckID 合并到任何地方的数据/视图中...您的查询可能类似于 data data = db.data.Where(x =&gt; x.TruckID = YourTruckIDVariable).ToList(),但您的视图也需要一些工作。 ..

标签: c# mysql asp.net asp.net-mvc azure


【解决方案1】:

有了这个答案,我假设两件事:

  • dbDbContext 并且有一个名为 data 的属性是 DbSet&lt;data&gt;
  • 您的数据没有任何其他模型,只有 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 Data 而不是data

【讨论】:

  • 您的假设是正确的,但是代码在详细信息页面上显示了一个空表。你知道为什么吗?另外,详细信息页面应该如何知道要显示哪些卡车 ID 行?
  • 这就是我的想法:索引页面显示记录 - 由 id 标识。有几行具有不同的id-s 但相同的truckID-s。我的解决方案找到带有选定id 的行,然后使用该记录的truckID 来收集其余数据。我确实意识到,我从db.data.Where(d =&gt; d.truckID == data.truckID) 的末尾错过了一个.ToList()。添加它或只是复制我更新的答案,这些行应该会显示出来。
  • 正确,索引显示具有唯一 ID 的每一行。每行也有一个卡车ID,但多行可以有相同的卡车ID。但是在实现您的代码后视图仍然是空的。是否还有其他代码可能需要 sn-p?
  • 所以更新:return View(db.data.Where(c =&gt; c.truckID == 100).ToList()); 这可以正常工作并检索所有带有 truckID 100 的行。所以我想我的问题是如何将 TruckID 从索引保留到详细信息?
  • 你能用调试器检查data.truckID的值吗?
【解决方案2】:

您没有收到关于此声明的任何错误?

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

我觉得应该是这样的

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

我猜你正在使用 foreach 循环来打印所有数据,并且在那个循环中你有 @Html.ActionLink。

【讨论】:

  • 没有错误。是的,索引上的 ActionLink 有一个 foreach 循环 @foreach (var item in Model)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 2012-08-03
相关资源
最近更新 更多