[英]How do I post the row id in a custom built ASP.NET Core Razor Pages editable Grid?
I have tried several approaches, and as I'm new to Razor Pages in ASP.NET Core I'm blocked.我尝试了几种方法,但由于我是 ASP.NET Core 中 Razor Pages 的新手,所以我被阻止了。 Could anybody help me please?有人可以帮我吗? It must be something trivial but I googled quite a lot and I can't find a suitable way.这一定是微不足道的,但我用谷歌搜索了很多,但找不到合适的方法。 I don't want to use jquery or any other javascript library.我不想使用 jquery 或任何其他 javascript 库。
Here is the relevant code:以下是相关代码:
<form method="post">
<table>
<thead>
<tr>
<th>
Text 1
</th>
<th>
Text 2
</th>
<th>
Text 3
</th>
</tr>
</thead>
<tbody>
@foreach(var item in Model.GridRows)
{
<tr>
<td>
<input type="hidden" asp-for="@item.Id" />
</td>
<td>
@Html.EditorFor(modelItem => item.Text1)
</td>
<td>
@Html.EditorFor(modelItem => item.Text2)
</td>
<td>
@Html.EditorFor(modelItem => item.Text3)
</td>
<td>
<input type="submit" value="Save" class="btn btn-primary"/>
</td>
</tr>
}
</tbody>
</table>
</form>
public async Task<IActionResult> OnPostAsync(int id)
{
if (!ModelState.IsValid)
{
return Page();
}
var gridRowUpdate = await _context.GridRows.FindAsync(id);
if (gridRowUpdate == null)
{
return NotFound();
}
if(await TryUpdateModelAsync<GridRow>(gridRowUpdate, "GridRow", g => g.Text1, g => g.Text2, g => g.Text3))
{
await _context.SaveChangesAsync();
}
return RedirectToPage("./Index");
}
IF you want to click each save
button to send the Id of that row, You can just follow below simple demo without any jquery or any other javascript library.如果您想单击每个save
按钮以发送该行的 Id,您可以按照下面的简单演示进行操作,无需任何 jquery 或任何其他 javascript 库。
<table>
<thead>
<tr>
<th>
Text 1
</th>
<th>
Text 2
</th>
<th>
Text 3
</th>
</tr>
</thead>
<tbody>
@foreach(var item in Model.test)
{
<form method="post" >
<tr>
<td>
<input type="hidden" name="Id" value="@item.Id" />
</td>
<td>
@Html.EditorFor(modelItem => item.Text1)
</td>
<td>
@Html.EditorFor(modelItem => item.Text2)
</td>
<td>
@Html.EditorFor(modelItem => item.Text3)
</td>
<td>
<input type="submit" value="Save" class="btn btn-primary"/>
</td>
</tr>
</form>
}
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.