【问题标题】:Pass multiple parameters in jquery ajax call在 jquery ajax 调用中传递多个参数
【发布时间】:2021-11-11 12:03:25
【问题描述】:

我对 jquery ajax 相当陌生,因此非常感谢任何帮助,我正在尝试将参数传递给我的控制器,但抛出一个异常,即我的参数为空。这是我的jQuery代码:

$("#btnSave1").click(function () {
            var ConfirmPasswordForm = $("#ConfirmPasswordForm").serialize();
            var Myid = $("#testPurpose").val();

            $.ajax({
                type:"POST",
                url: "/Folder/ConfirmPassword",
                data: {viewModel: ConfirmPasswordForm, folderPath: Myid},
                success: function () {
                    window.location.href = "/Folder/GetFolders1"
                }
            })
        });

这是我在控制器中的功能:

    [HttpPost]
    public void ConfirmPassword(FoldersandFilesViewModel viewModel, string folderPath)
    {
        var userId = User.Identity.GetUserId();
        ApplicationUser user1 = UserManager.FindById(userId);
        var result = UserManager.CheckPassword(user1, viewModel.Password);
        if (result)
            DeleteFolder(folderPath);
    }

【问题讨论】:

  • 确认密码表单是FoldersandFilesViewModel类型的表单,这是Myid变量
  • 它的值在这里设置:$(".myCustomContainer").on("click", ".my-delete", function () { var button = $(this); $(" #ConfirmPasswordModal").modal('show'); $("#testPurpose").val(button.attr("data-folder-path")); });

标签: jquery ajax asp.net-mvc asp.net-ajax


【解决方案1】:

你必须创建一个视图模型

public class FormViewModel
{
public FoldersandFilesViewModel ViewModel {get; set;]
public string FolderPath {get; set;}
}

动作

public IActionResult ConfirmPassword(FormViewModel formViewModel)
{
.....
return Ok();
}

【讨论】:

    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多