'How to get multiple IDs on click from table row?
I wanna to get multiple IDs for delete method, I have a table, each cell has different Id I need to get those IDs and be able to delete them one by one.So there is 5 columns, 5 different IDs in one row here is my code what I tried to do:
Controller:
[HttpGet]
[Route("MIDKalorimetar/Delete/{Id:int}/{Id2:int}/{Id3:int}/{Id4:int}/{Id5:int}")]
public async Task<IActionResult> Delete(int Id, int Id2, int Id3, int Id4, int Id5)
{
var model = await _db.ParametriMjerila.Where(x => x.Id == Id && x.Id == Id2 && x.Id == Id3 && x.Id == Id4 && x.Id == Id5).FirstOrDefaultAsync();
return PartialView("Delete", model);
}
[HttpPost]
public async Task<IActionResult> Delete(DeleteKalorimetarVM parametrniMjerila)
{
var model = await _db.ParametriMjerila.Where(x => x.Id == parametrniMjerila.Id).FirstOrDefaultAsync();
_db.ParametriMjerila.Remove(model);
_db.SaveChanges();
return RedirectToAction("Index", model);
}
Index.cshtml:
<tbody style="text-align:center;">
@{
for(int x = 0; x < Model.NajveciBrojElemenata; x++){
<tr id="@Model.Id">
<td>@Model.Qi.ElementAtOrDefault(x)?.VrijednostKarakteristike</td>
<td>@Model.Qp.ElementAtOrDefault(x)?.Opis</td>
<td>@Model.Qs.ElementAtOrDefault(x)?.VrijednostKarakteristike</td>
<td>@Model.R.ElementAtOrDefault(x)?.VrijednostKarakteristike</td>
<td>@Model.SP.ElementAtOrDefault(x)?.VrijednostKarakteristike</td>
@if (dozvoliEdit)
{
<td style="width:20%">
<button class="bg-transparent border-0" style=" background: transparent; border: 0; border: 0 !important; " data-toggle="ajax-modal" data-url="@Url.Action($"Details/")">
<img src="/images/detailsPng.png" width="25" height="25">
</button>
<button class="bg-transparent border-0" style=" background: transparent; border: 0; border: 0 !important; " data-toggle="ajax-modal" data-url="@Url.Action($"Uredi/")">
<img src="/images/editPng.png" width="25" height="25">
</button>
<button class="bg-transparent border-0" style=" background: transparent; border: 0; border: 0 !important; " data-toggle="ajax-modal" data-url="@Url.Action($"Delete/{@Model.Id}")">
<img src="/images/deletePng.png" width="25" height="25">
</button>
</td>
}
</tr>
}
}
</tbody>
View model for Index.cshtml:
public int[] Id { get; set; }
public List<ParametarMjerila> IDs { get; set; }
public List<ParametarMjerila> Qp { get; set; }
public List<ParametarMjerila> Qi { get; set; }
public List<ParametarMjerila> R { get; set; }
public List<ParametarMjerila> Qs { get; set; }
public List<ParametarMjerila> SP { get; set; }
public int NajveciBrojElemenata
{
get; set;
} = 16;
How to pass those IDs to data-url="@Url.Action($"Delete/{@Model.Id}")" to open modal delete. The error I get is 405 I cant pass IDs
Solution 1:[1]
I see your action Delete is async Task so you need to await the database query too
await _db.ParametriMjerila.Remove(model);
await _db.SaveChanges();
But I also advise you to move all your logic to a service class.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Darkk L |
