0

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!

2
  • Does the delete operation really need to receive the entire object, or can it just receive an identifier for the object? Normally one would just use an ID value to delete a record from data. Commented Apr 6, 2022 at 15:11
  • @David It can also receive the identifier of the object, that would work fine. Commented Apr 6, 2022 at 15:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.