简体   繁体   English

单击删除按钮重定向到日志记录页面

[英]Clicking Delete button redirects to Loging page

I am using BeginCollectionItem to dynamically add items to a list.我正在使用BeginCollectionItem将项目动态添加到列表中。 If I attempt click a newly added row, the app immediately halts and redirects me to the login page.如果我尝试单击新添加的行,应用程序会立即停止并将我重定向到登录页面。 If I delete an item that already existed, it deletes it from the database, then deletes it from the UI, then redirects me to the login page.如果我删除一个已经存在的项目,它会从数据库中删除它,然后从 UI 中删除它,然后将我重定向到登录页面。 If the item is newly added, it does not even run the code in the delete.onClick at all the other does, but they both end up with redirecting me to the login screen.如果该项目是新添加的,它甚至不会运行 delete.onClick 中的代码,但它们最终都将我重定向到登录屏幕。

I had a similar problem when trying append the div and was told to add:我在尝试 append 时遇到了类似的问题,并被告知要添加:

                e.preventDefault();
                e.stopImmediatePropagation();

I tried that but it did not stop my app from halting and redirecting to the login page.我试过了,但它并没有阻止我的应用程序停止并重定向到登录页面。 Here is my delete code:这是我的删除代码:

    $("a.deleteRow").on("click", function (e) {

    var container = $(this).closest('.editorRow');
    var id = container.data('id');
    var $t = $(this);

        $.ajax({
            type: "POST",
            url: "@Url.Action("Delete","Request", new { area = "" })",
            data: { CommentId: id },
            success: function (e) {
                $t.parents("div.editorRow:first").remove();
                e.preventDefault();
                e.stopImmediatePropagation();
            }
        })

    return false;
});

Here is the partial view:这是部分视图:

@using (Html.BeginCollectionItem("Comments"))
{
        <div class="row editorRow" data-id="@Model.Id" style="padding-bottom:10px">
            @Html.HiddenFor(x => x.Id)

            <div class="col-xl-2">
                @if (Model.CreatorName == "" || Model.CreatorName == null)
                {
                    Html.DisplayFor(x => x.CreatorName, (string)@ViewBag.UserName);
                }
                else
                {
                    @Html.DisplayFor(x => x.CreatorName)

                }
            </div>
            <div class="col-xl-8">
                @Html.TextAreaFor(x => x.Comment, new { @class = "form-control" })
            </div>
            <div class="col-xl-2">
                <a href="#" class="deleteRow">delete</a>
            </div>
        </div>
}

here is Delete code in Controller:这是 Controller 中的删除代码:

        public ActionResult Delete(int CommentId)
    {
        service.DeleteComment(CommentId);
        return Content("" + CommentId);

    }

Update:更新:

After putting the delete code in document.ready, the ajax portion deleting the existing row now works fine.将删除代码放入 document.ready 后,删除现有行的 ajax 部分现在可以正常工作。 However, attempting to add and delete a row without first saving that row is still redirecting.但是,在不先保存该行的情况下尝试添加和删除该行仍然是重定向。 The new delete code which now works with an "existing" row is as follows:现在适用于“现有”行的新删除代码如下:

   $(document).ready(function () {
    $("a.deleteRow").on("click", function (e) {

        var container = $(this).closest('.editorRow');
        var id = container.data('id');
        var $t = $(this);

        $.ajax({
            type: "POST",
            url: "@Url.Action("Delete","Request", new { area = "" })",
            data: { CommentId: id },
            success: function (e) {
                $t.parents("div.editorRow:first").remove();
                return false;
            }
        })

        return false;
    });
});

I am new to jQuery and am trying to understand as well as get my project working.我是 jQuery 的新手,我正在努力理解并让我的项目正常工作。

Looking at the code I cannot see that this redirection is related to jquery.查看代码我看不出此重定向与 jquery 相关。 Can you take a look at the RequestController and Delete action.你能看一下 RequestController 和 Delete 操作吗? Most likely your Delete Action in controller is doing the redirection in this case after it has deleted the record from database.在这种情况下,您在 controller 中的删除操作很可能在从数据库中删除记录后进行重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM