admin管理员组

文章数量:1129091

I have a button that opens a modal that I use to edit a name.

<button type="button" data-toggle="modal" 
        OnClick="showInPopup('@Url.Action("EditName", "ChangeRequest", new { Id = sf.Id }, Context.Request.Scheme)', 'Edit Name9')" 
        data-target="#modalDiv" class="btn btn-sm btn-info">
    <i class="fas fa-pencil-alt"></i>
</button>

Here is the modal

<!-- Edit Folder Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Edit Name</h5>
            </div>
            <div class="modal-body" id="modalContent">
                ...
            </div>
        </div>
    </div>
</div>

This is the ajax script

showInPopup = (url, title) => {
    $.ajax({
        type: 'GET',
        url: url,
        success: function (res) {
            $('#editModal .modal-body').html(res);
            $('#editModal .modal-title').html(title);
            $('#editModal').modal('show');
            // to make popup draggable
            //$('.modal-dialog').draggable({
            //    handle: ".modal-header"
            //});
        }
    })
}

And this is the partial view inside the modal:

@model RequestViewModel

@{
    Layout = null;
}

<div class="row">
    <form asp-action="EditName" asp-route-id="@Model.SharedFile.Id" onsubmit="return jQueryAjaxPost(this);">
        <fieldset>
            <div class="form-group py-3">
                @Html.HiddenFor(x => x.SharedFile.Id)
                <h3>New Folder Name</h3>
                @* <label for="txtNewFoldName" class="form-label mt-4">New Folder Name</label> *@
@*                 <input asp-for="SharedFile.FileName" class="form-control" name="fileName"/> *@
                <input class="form-control" id="=txtNewFoldName" name="fileOrFolderName" value="@Model.SharedFile.FileName">
            </div>
        </fieldset>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Save changes</button>
        </div>
    </form>
</div>

The submit button is handled with this ajax script.

jQueryAjaxPost = form => {
    try {
        $.ajax({
            type: 'POST',
            url: form.action,
            data: new FormData(form),
            contentType: false,
            processData: false,
            success: function (res) {
                if (res.isValid) {
                    $('#editModal .modal-body').html('');
                    $('#editModal .modal-title').html('');
                    $('#editModal').modal('hide');
                }
                else
                    $('#form-modal .modal-body').html(res.html);
            },
            error: function (err) {
                console.log(err)
            }
        })
        //to prevent default form submit event
        return false;
    } catch (ex) {
        console.log(ex)
    }
}

Finally here is the controller method to handle it:

[HttpPost]
public ActionResult EditName(int id, string fileOrFolderName, BaseViewModel bvm)
{
    RequestViewModel rvm = ChangeRequest.SubmitEditFileOrFolderNameRequest(bvm.SharedFile, fileOrFolderName, User.Identity.Name, _memoryCache);
    rvm.SharedFile = DAL.GetFolderContents(id);

    return View("Confirmation", rvm);
}

This ALL works as expected. My data is returned in the POST method correctly and my breakpoint is being hit on the final line

return View("Confirmation", rvm);

which I expect to take me to this page and display a confirmation message.

But instead, no redirect happens. Is this because I am calling from a partial view inside the modal?

I have tried to use RedirectToAction instead - without success. I have other button on the page that do not use a partial view and take me to the confirmation page as expected.

本文标签: cHttpPost does not redirect to ViewStack Overflow