MVC URL.ACTION HREF ONCLICK确认不会阻止重定向

发布于 2025-01-19 03:01:24 字数 1275 浏览 5 评论 0原文

问题:onclick =“返回确认('您确定吗?')”我的HREF中的html属性总是删除标签,无论用户选择什么选择。

以下是我的Deletetag.cs​​html文件的片段。我正在使用url。操作产生要删除给定标签的路径。我正在使用 @锚定标签来使用URL返回url。

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>
}

当该URL点击(无论是继续或取消)时,以下代码执行。

[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();
  }
}

在这里,提示了确认消息,我的选择是取消。 “在此处输入映像说明”

即使我选择了CANCAL,代码仍会受到命中。 “在此处inter

我希望我会返回false,如果我选择取消,则不应命中url,并且该网址不应命中,并且删除标签代码不应执行。

也许我有些误解。

编辑:我还尝试过onclick ='return false;'这仍然导致执行删除标签操作结果

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文