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

时间:2017-08-25 11:09:20

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

“我的索引”页面显示Azure SQL数据库中的所有行。当我点击一行时,我会被重定向到一个带有该URL的QR码的唯一URL,它还会显示该行的数据。每行都有自己的唯一ID,但也有一个truckID。 truckID适用于能够拥有"几行数据,所以我想在点击一行后显示具有相同truckID的每一行。

这是我到目前为止所得到的:

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(从索引点击一行后)

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

在索引上我有这个链接:

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

帮助?

2 个答案:

答案 0 :(得分:1)

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

  • dbDbContext,并且有一个名为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 Data而不是data

答案 1 :(得分:-1)

您在此声明中没有收到任何错误?

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

我认为必须像这样

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

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