0

I am having trouble deleting a row by Id. I display every row of my DB in a table and next to every row I have a button that should be used for deleting that specific row in the DB.

It should delete only the row with that specific id, but what actually happens, is that I don't even press the button, but when accessing the page - the whole content of the DB gets deleted.

What did I do wrong?

Here's my code so far:

Controller:

public class AdminController : BaseController
{
    private UserContext context;

    public AdminController()
    {
        context = new UserContext();
    }

    public void delete_by_id(int id)
    {
        context.Users.Where(x => x.Id == id).DeleteFromQuery();
    }

    // GET: Admin
    [AdminMod]
    public ActionResult Index()
    {
        SessionStatus();
        if ((string)System.Web.HttpContext.Current.Session["LoginStatus"] != "login")
        {
            return RedirectToAction("Index", "Login");
        }
        var user = System.Web.HttpContext.Current.GetMySessionObject();
        UserData u = new UserData
        {
            Username = user.Username,
            Level = user.Level,
        };


        return View("Index", u);
    }
}

UserContext:

public class UserContext : DbContext
{
    public UserContext() :
        base("name=WebApplication1")
    {

    }

    public virtual DbSet<UDbTable> Users { get; set; }
}

Index.cshtml:

@using WebApplication1.Controllers
@using WebApplication1.Domain.Enums
@using WebApplication1.Extension
@using WebMatrix.Data


@{
    ViewBag.Title = "Admin";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var db = Database.Open("WebApplication1");
    var selectQueryString = "SELECT * FROM UDbTables ORDER BY Id";

}

<script>
    function refreshPage() {
        window.location.reload();
    }
</script>

<div class="container" style="margin-top: 1%;">
    <h1>AdminPage</h1>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th scope="col">#Id</th>
            <th scope="col">Username</th>
            <th scope="col">Email</th>
            <th scope="col">Level</th>
            <th scope="col"></th>
        </tr>
        </thead>
        <tbody>
        @foreach (var row in db.Query(selectQueryString))
        {
            <tr>
                <th scope="row">#@row.Id</th>
                <td>@row.Username</td>
                <td>@row.Email</td>
                @{
                    if (@row.Level == 0)
                    {
                        <td>User</td>
                    }
                    else if (row.Level == 1)
                    {
                        <td>Premium</td>
                    }
                    else
                    {
                        <td>Admin</td>
                    }
                }
                <td>
                    <button type="button" data-bs-togle="modal" data-bs-target="#IdModal" class="btn btn-danger" data-toggle="tooltip" data-placement="right" title="Delete User" id="@row.Id" onclick ="refreshPage()">
                        @{
                            var smth = new AdminController();
                            smth.delete_by_id(row.Id); //HERE IS WHERE I CALL THE QUERY
                        }
                        <i class="fas fa-trash"></i>
                    </button>
                </td>
            </tr>

          }
        </tbody>
    </table>
</div>

Here I have the proof that the correct Id is assigned to each row:

1st 2nd

3
  • Code provided on a .cshtml page between @{...} is executed server side . Your smth.delete_by_id(row.Id) is executed before the page is even loaded, and that for each row. Commented Apr 29, 2022 at 12:55
  • Thanks for the advice! How should/could I fix that? Commented Apr 29, 2022 at 12:56
  • from an MVC point of view you are doing many things wrong. I suggest you to work through some tutorials for example here learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/… Commented May 1, 2022 at 8:37

1 Answer 1

1

Create a form for submitting a POST request for delete action.

<td>
    @using (Html.BeginForm("Delete", "Admin", new { id = row.Id }, FormMethod.Post))
    {
        <button type="submit" data-bs-togle="modal" data-bs-target="#IdModal" class="btn btn-danger" data-toggle="tooltip" data-placement="right" title="Delete User" id="@row.Id">
            <i class="fas fa-trash"></i>
        </button>
    }
</td>

In AdminController, you need a Delete with [HttpPost] method to perform the delete operation and then return View.

public class AdminController : BaseController
{
    ...

    [HttpPost]
    public ActionResult Delete(int id)
    {
        delete_by_id(id);

        // Return desired view
        return Index();
    }
} 
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.