S
S
Saharman2018-03-28 11:43:21
ASP.NET
Saharman, 2018-03-28 11:43:21

How to implement ASP.NET Core Ajax delete?

The essence of the task is this - it is necessary to implement deletion on ASP.net core without reloading the page using Ajax. Ajax tags, unfortunately, do not work in core. And if possible, then the implementation with a minimum of javascript code is better.
Here is the code from the view:

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#results" href="/Project/[email protected]">Удалить</a>

Here is the controller:
public ActionResult Delete(int? id)
        {
            Project project = _context.Project.Find(id);
            _context.Project.Remove(project);
            _context.SaveChanges();
            return PartialView("newList");
        }

The error is thrown in the line _context.SaveChanges();
5abb74326bbb1396420901.png

UPD!!!
I found the cause of the problem: for some reason, when you click on delete, two requests are sent to the controller for some unknown reason, and one object is deleted, and the second attempt to delete it leads to an error. Please help me to solve the problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrDywar Pichugin, 2018-03-28
@Dywar

Look at courses like this - https://codewithmosh.teachable.com/p/asp-net-core/...
This course (3 pcs.) is just super - https://programmingwithmosh.com/become-a- full-stac...
After studying it, you will learn a lot of new things.
It’s better to immediately learn how to do small projects right from and to, then it’s easier on your own.
To avoid writing Delete(int? id) and the like (this method is useless with a null argument, it sounds like "delete nothing").

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question