【问题标题】:Accidentally redirecting after displaying Bootstrap modal显示 Bootstrap 模态后意外重定向
【发布时间】:2017-07-07 17:34:19
【问题描述】:

我认为标题很清楚,但可以肯定的是:我不希望在显示模态后重定向,这是我的搜索产生的唯一结果。

页面错误地重定向到其他地方,但我不知道为什么。

我有一个条目列表(基本上只是 MVC 生成的脚手架 CRUD),当单击按钮时,会弹出一个模式并通过 AJAX 显示一些信息(最终它将是一个表单,但那是现在不重要)。模态只是瞬间弹出,然后我被重定向到Details 页面之一。

模态应该使用来自Archive/{id} 的部分视图,它确实如此,但随后我立即被发送到Details/{id}。我不知道我在哪里告诉重定向发生。

我正在使用{controller}/{action}/{id} 的默认路由。 Chrome 调试器没有显示任何错误。

_ArchiveModal.cshtml

@model EDB.Models.ProductModel

<div>

    <div class="modal-header">
        <button type="button" class="close btn-modal-close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="archiveModalLabel">Archive?</h4>
    </div>

    <hr />

    <p>
        Test test testing all day, these are words
    </p>

</div>

div in Index.cshtml

<div id="archiveModal" class="modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div id="archiveModalContent"></div>
        </div>
    </div>
</div>

JavaScript / jQuery

$(document).ready {
    $(".modal-anchor-archive").click(function () {
        var modalUrl = '@Url.Action("Archive")' + "/" + $(this).data("id");
        var options = {
            "backdrop": "static",
            keyboard: true
        };
        $.ajax({
            type: "GET",
            url: modalUrl,
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            success: function (data) {
                $("#archiveModalContent").html(data);
                $("#archiveModal").modal(options);
                $("#archiveModal").modal("show");
                debugger; // Stopping here shows that the modal does display
            },
            error: function (error) {
                alert("Dynamic content load failed.");
            }
        });

        $(".btn-modal-close").click(function () {
            $("#archiveModal").modal("hide");
        });
    });
}

按钮

<span class="btn btn-warning modal-anchor-archive" data-id="@item.ID">Archive</span>

控制器动作

public ActionResult Archive(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    ProductModel productModel = db.Products.Find(id);
    if (productModel == null)
    {
        return HttpNotFound();
    }
    return PartialView("_ArchiveModal", productModel);
}

【问题讨论】:

  • 你的动作方法是什么样的?您是否有任何错误逻辑 - 例如,错误直接到“详细信息”页面?
  • @wahwahwah 意识到我在你打字的时候忘记了这一点。我没有任何我知道的错误逻辑,除非在某处生成了某些东西(除了 action 方法中的一点点逻辑)。
  • 既然您说模型显示为即时使用,例如 "$(".modal-anchor-archive").click(function (e) { " 并且成功时 e.preventDefault (); 以便单击按钮不会提交表单或重定向到任何页面。
  • @Shan 喜欢this?不确定这是否正确,但它没有做任何事情。
  • 您的 ExceptionModel 是否包含代码中抛出的实际异常?也许其中一个被抓住了,这会将您重定向到详细信息页面。我不确定我是否只是在这里寻找一些东西。

标签: javascript jquery asp.net-mvc twitter-bootstrap bootstrap-modal


【解决方案1】:

在这里。

原来这是 jQuery 的 event propagation 功能的问题。包含模式按钮的行注册了一个onClick 事件,该事件重定向到Details。我不敢相信这让我失去了理智。

修复非常简单。

$(".modal-anchor-archive").click(function (e) {
    // Prevent the event from "bubbling up" to this element's container(s)
    e.stopPropagation();
    // Other code
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    相关资源
    最近更新 更多