1

HttpPost on delete action does not trigger, however HttpGet seems working fine as i get the content displayed. However I have little confusion in the following route address generated when I click on HttpGet on delete action:-

https://localhost:44394/9

shouldn't it generates link like this: https://localhost:44394/Post/DeletePost/9

Controller:-

[HttpPost, ActionName("DeletePost")]
public async Task<IActionResult> ConfirmDelete(int id)
{          
     await _repository.DeletePostAsync(id);             
     return RedirectToAction(nameof(GetAllPosts));           
 }
[HttpGet("{id}")]
public async Task<IActionResult> DeletePost(int id)
{
  var post = await _repository.GetPostById(id);
  if(post == null)
   {
     return NotFound();
   }
    return View(post);
}

Razor View for HttpGet:-

 <div class="btn btn-outline-danger delete">
    <a href="@Url.Action("DeletePost", "Post", new { id = p.Id })">Delete
    </a>
</div>

Razor Page HttpPost:-

<div class="container">
    <div class="row">
        <div class="col-9">
            <p>
                @Model.Topic
            </p>
            <p class="timeStampValue" data-value="@Model.Published">
                @Model.Published
            </p>
            <p>
                @Model.Author
            </p>
            <section>
                <markdown markdown="@Model.Content" />
            </section>
        </div>
    </div>

    <form asp-action="DeletePostAsync"> 
        <input type="hidden" asp-for="Id" />
        <button type="submit" class="btn btn-outline-danger">Delete</button>
    </form>
    <a href="@Url.Action("GetAllPosts", "Post")" class="btn btn-outline-secondary">Cancel</a>
</div>

Routing:-

app.UseMvc(routes =>
{     routes.MapRoute(
      name: "KtsPost",
      template: "{controller}/{action}/{id?}",
      defaults: new { controller = "Post", action = "Index" },
     constraints: new { id = "[0-9]+" });
      });

2 Answers 2

2

Your action name is wrong in the form. Your code should be instead:

<form asp-action="DeletePost"> 
    <input type="hidden" asp-for="Id" />
    <button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

The default method of an HTML form is GET not POST. You need to tell your form to POST. Also, the action name should be ConfirmDelete:

<form asp-action="ConfirmDelete" method="post"> 
    <input type="hidden" asp-for="Id" />
    <button type="submit" class="btn btn-outline-danger">Delete</button>
</form>

1 Comment

Thanks for your time and help, however i have already tried all these steps that you have suggested. In fact my HttpPost ActionName is DeletePost, shouldn't it trigger the method "ConfirmDelete", though i changed it as you suggested but does not make any different. When I tried to debug not it does not trigger HttpPost action method..

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.