I have tried several approaches, and as I'm new to Razor Pages in ASP.NET Core I'm blocked. 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.
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" />
</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");
}
CodePudding user response:
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.
<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" />
</td>
</tr>
</form>
}
</tbody>
</table>