jQuery ajax上的多个参数调用asp.net [英] Multiple parameters on jquery ajax call asp.net

查看:96
本文介绍了jQuery ajax上的多个参数调用asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ajax调用中传递两个参数.我已经尝试过在StakeOverflow上建议的几种方法,但没有一种起作用.这是我在控制器上的方法签名:

I am trying to pass two parameters on an ajax call. I already tried several ways suggested on StakeOverflow but none worked. Here is my method signature on controller:

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase[] files, string[] usersToShare)

这是我的功能:

function uploadFile() {
        var formData = new FormData();
        var totalFiles = document.getElementById("files").files.length;

        for (var i = 0; i < totalFiles; i++) {
            var file = document.getElementById("files").files[i];
            formData.append("files", file);
        }

        //get the selected usernames (email) to share the file
        var selectedUsers = [];
        $("#costumerUsersListSelect :selected").each(function () {
            selectedUsers.push($(this).val());
        });

        $.ajax({
            type: 'post',
            url: '/ManageFiles/UploadFile', 
            data: "files=" + formData + "usersToShare=" + selectedUsers,
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function (data) {                    
            },
            error: function (error) {                  
            }
        });
    }

所以我想将 formData selectedUsers 传递给控制器​​.如果我只传递formData(数据:formData),那么一切都可以,但是我也需要传递selectedUsers.

So I want to pass to the controller the formData and the selectedUsers. If I pass just the formData (Data: formData) everything works but I need to pass the selectedUsers too.

这是我已经尝试过但没有成功的东西:

Here what I already tried without any success:

data: JSON.stringify({ files: formData, usersToShare: selectedUsers }),
data: { files: formData, usersToShare: JSON.stringify(selectedUsers)},
data: "files=" + formData + "&usersToShare=" + selectedUsers,
data: "files=" + formData + "usersToShare=" + selectedUsers,

我不确定这是否是语法问题.

I am not sure if this is a syntax issue.

在此先感谢您的帮助.

推荐答案

而不是:

data: "files=" + formData + "usersToShare=" + selectedUsers,

formData中附加selectedUsers,仅将formData发送到服务器,例如:

append the selectedUsers in formData and send only formData to the server like:

data: formData, 

然后重试.

这篇关于jQuery ajax上的多个参数调用asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆