Run Action Post by Jquery ASP.NET MVC

Asked

Viewed 769 times

0

Would you like to delete a record in a button click by Jquery, what’s the best way to do it ? The way I did it’s not working.

Action I want to call by Jquery

        // POST: Pais/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Pais pais = db.Paises.Find(id);
            pais.Delete();
            return RedirectToAction("Index");
        }

Jquery event where I want to call Action Deleteconfirmed

 $('.btnExcluirSimPopover').on('click', function () {
    if (handlePopover > 0) {
      $.post("/Pais/DeleteConfirmed", { id: handlePopover }, function (data){

      });
    }
 });
  • Are you making a mistake or something? can you report better?

  • @Rafaelaugusto, no error friend, just do not enter the action I’m giving the post.

1 answer

1


The date Annotation ActionName is overwriting the name of your action DeleteConfirmed for Delete. Change that name on your call that should work.

 $('.btnExcluirSimPopover').on('click', function () {
    if (handlePopover > 0) {
      $.post("/Pais/Delete", { id: handlePopover }, function (data){

      });
    }
 });
  • 1

    That’s right, but I also had to take out the [Validateantiforgerytoken], it worked.

  • It’s true. To work with the [ValidateAntiForgeryToken] you need to send the token generated by Html.AntiForgeryToken() in your request also!

  • If the answer has met you, please signal it as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.