Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
MVC Url.Action Href onclick Confirmation not preventing redirect

Problem: The onclick="return confirm('Are you sure?')" HTML Attribute in my Href always deletes tag regardless of choice selected by user.

Below is a snippet of my DeleteTag.cshtml file. I am using Url.Action to produce a path to a given tag to be deleted. I am using an @ anchor tag in order to use the url return by Url.Action.

if (tag.CreatedByUserId == Model.ActiveUserId)
{
  // /Content/DeleteTag/5329
  var deleteUrl = Url.Action(MVC.Content.DeleteTag(tag.TagId));
  <a href="@deleteUrl" class="cw-delete-tag-link" rel="tagLocatorClass" onclick="return confirm('Are you sure?')" title="Delete this tag"><img src="../../public/images/icons/silk/delete.png" alt="Delete" /></a>
}

When this url is hit (regardless of continue or cancel), the following code executes.

[RequireAuthorization(MinAccessLevel = Roles.Admin)]
[HttpPost]
public virtual ActionResult DeleteTag(int id) 
{
  ContentAdminService.DeleteTag(id);

  if (Request.IsAjaxRequest()) {
    return Json(id, JsonRequestBehavior.AllowGet);
  } else {
    this.FeedbackInfo("Tag deleted");
    return View();
  }
}

Here the confirmation message is prompted and my choice is cancel. enter image description here

Here the code is still hit even though I selected cancel. enter image description here

I am expecting onclick to return false if I select cancel, and the url should not be hit, and the delete tag code should not be executed.

Perhaps there is something I am misunderstanding.

EDIT: I have also tried onclick='return false;' which still results in the execution of the delete tag action result

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda