I have been looking at other similar questions to this, but I can't seem to find a way to get this working. My view is being passed into a list of tAdminAlert objects that I list out in a table. I want a Delete button next to each row in the table that redirects to the Delete(tAdminAlert obj) action method in my controller and pass in the object corresponding to the row that they clicked.
My controller is called AdminAlerts and has this action method:
public ActionResult Delete(tAdminAlerts obj) {
// Want to call delete method on obj here
return View("Index");
}
My view looks like
@model List<tAdminAlerts>
@{
ViewData["Title"] = "Alerts";
}
@{
ViewBag.Title = "Alerts";
}
<div style="width:60%;display:table">
<table>
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Dept. Number</th>
<th>Dept. Code</th>
</tr>
</thead>
<tbody>
@foreach (var obj in Model) {
<tr>
<td width="35%">
@obj.nameFull
</td>
<td width="25%">
@obj.emailAddress
</td>
<td width="20%">
@obj.departmentNumber
</td>
<td width="20%">
@obj.departmentCode
</td>
<td>
* Want delete button to go here and return @obj
to Delete action method of controller *
</td>
</tr>
}
</tbody>
</table>
</div>
I would appreciate any help with this. Thanks!
CodePudding user response:
You don't need the entire object here:
public ActionResult Delete(tAdminAlerts obj)
You just need an identifier for the record being deleted, which you'd then use to delete that record in the data:
public ActionResult Delete(int id)
You can then just create a simple link to that action. Different versions of ASP.NET MVC may have slight differences, but in general you could create an Action Link with the link text, action name, (optional) controller name, and route values. For example:
@Html.ActionLink("Delete", "Delete", new { id = obj.id })
(I'm assuming the identifier on obj
in the view is called id
, use whatever identifier you want to use.)
This would just create a simple link for the user to click on to take them to that Delete
action.