MVC URL.ACTION HREF ONCLICK确认不会阻止重定向
问题:onclick =“返回确认('您确定吗?')”我的HREF中的html属性总是删除标签,无论用户选择什么选择。
以下是我的Deletetag.cshtml文件的片段。我正在使用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,代码仍会受到命中。
我希望我会返回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.
Here the code is still hit even though I selected cancel.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论