Saya miliki dengan log-pesan. Dan jika saya ingin pergi ke halaman detail. Id selalu kembali sebagai nilai "1".

Apa yang saya salah?

Halaman html (tampilan).

@model List<LogBerichtModel>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link href="css/styles.css" rel="stylesheet" />
</head>
<body>
    @using (Html.BeginForm("DetailInfo", "Home", FormMethod.Post))
    {
        <div class="container">
            <div><h4>Logberichten</h4></div>
            <div class="row logs" id="app">
                <div class="mt-5 d-flex align-content-xl-stretch flex-column p-5">
                    <table class="table table-striped m-2">
                        <thead>
                            <tr class="bg-primary-custom">
                                <th>@Html.DisplayNameFor(Model => Model[0].Bericht)</th>
                                <th>@Html.DisplayNameFor(Model => Model[0].Datum)</th>
                                <th>@Html.DisplayNameFor(Model => Model[0].Systeem)</th>
                                <td>@Html.DisplayNameFor(Model => Model[0].Klasse)</td>
                                <td>@Html.DisplayNameFor(Model => Model[0].Methode)</td>
                                <td>@Html.DisplayNameFor(Model => Model[0].Systeemmelding):</td>
                                <td></td>
                            </tr>
                        </thead>
                        @for (var item = 0; item < Model.Count; item++)
                        {
                            <tbody>
                                <tr style='background-color:@(item%2 == 0 ? "#FEF2F4":"#FEE2CA"  );'>
                                    <td>@Html.DisplayFor(Model => Model[item].Bericht)</td>
                                    <td>@Html.DisplayFor(Model => Model[item].Datum)</td>
                                    <td>@Html.DisplayFor(Model => Model[item].Systeem)</td>
                                    <td>@Html.DisplayFor(Model => Model[item].Klasse)</td>
                                    <td>@Html.DisplayFor(Model => Model[item].Methode)</td>
                                    <td>@Html.DisplayFor(Model => Model[item].Systeemmelding)</td>
                                    <td>
                                        <input id="id" name="id" type="hidden" value="@Model[item].Id" />
                                        <input class="bg-primary-custom" type="submit" value="Submit" title="DetailInfo" />
                                    </td>
                                </tr>
                            </tbody>
                        }
                    </table>
                </div>
            </div>
        </div>
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    }
</body>

Di controller (id selalu 1)

    [HttpPost]
    public IActionResult DetailInfo(int id)
    //public IActionResult DetailInfo(LogBerichtModel logbericht)
    {
0
user1531040 20 April 2021, 12:55

1 menjawab

Jawaban Terbaik

Lingkaran Anda ada dalam formulir, dan tindakan pengiriman Anda adalah untuk pengiriman seluruh formulir.

Formulir Anda berisi semua id, jadi saat Anda mengirimkan, Anda mengirimkan kumpulan id.

Saya sarankan Anda menggunakan @Html.ActionLink.

Ubah View Anda seperti berikut.

@model List<LogBerichtModel>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link href="css/styles.css" rel="stylesheet" />
</head>
<body>
<div class="container">
        <div><h4>Logberichten</h4></div>
        <div class="row logs" id="app">
            <div class="mt-5 d-flex align-content-xl-stretch flex-column p-5">
                <table class="table table-striped m-2">
                    <thead>
                        <tr class="bg-primary-custom">
                             <th>@Html.DisplayNameFor(Model => Model[0].Bericht)</th>
                            <th>@Html.DisplayNameFor(Model => Model[0].Datum)</th>
                            <th>@Html.DisplayNameFor(Model => Model[0].Systeem)</th>
                            <td>@Html.DisplayNameFor(Model => Model[0].Klasse)</td>
                            <td>@Html.DisplayNameFor(Model => Model[0].Methode)</td>
                            <td>@Html.DisplayNameFor(Model => Model[0].Systeemmelding):</td>
                        <td></td>
                        </tr>
                    </thead>
                    @for (var item = 0; item < Model.Count; item++)
                    {
                        <tbody>
                            <tr style='background-color:@(item%2 == 0 ? "#FEF2F4":"#FEE2CA"  );'>
                                <td>@Html.DisplayFor(Model => Model[item].Bericht)</td>
                                <td>@Html.DisplayFor(Model => Model[item].Datum)</td>
                                <td>@Html.DisplayFor(Model => Model[item].Systeem)</td>
                                <td>@Html.DisplayFor(Model => Model[item].Klasse)</td>
                                <td>@Html.DisplayFor(Model => Model[item].Methode)</td>
                                <td>@Html.DisplayFor(Model => Model[item].Systeemmelding)</td>
                                <td>
                                    @Html.ActionLink("DetailInfo", "DetailInfo", "Home",  new { id = @Model[item].Id }, null)
                                </td>
                            </tr>
                        </tbody>
                    }
                </table>
            </div>
        </div>
    </div>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
</body>

Dan ubah metode Anda menjadi get.

 [HttpGet]
 public IActionResult DetailInfo(int id)
{
}

Hasil tes:

enter image description here

1
Yinqiu 21 April 2021, 01:45