Reputation: 2291
I have with log-messages. And if I want to go to the detailpage. The Id returns always as the value "1".
What do I wrong?
The html-page (view).
@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>
In the controller (id is always 1)
[HttpPost]
public IActionResult DetailInfo(int id)
//public IActionResult DetailInfo(LogBerichtModel logbericht)
{
Upvotes: 0
Views: 54
Reputation: 7190
Your loop is in the form, and your submission action is for the submission of the entire form.
Your form contains all the ids, so when you submit, you submit a collection of ids.
I suggest you use @Html.ActionLink
.
Change your View
like following.
@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>
And change your method to get
.
[HttpGet]
public IActionResult DetailInfo(int id)
{
}
Test result:
Upvotes: 1