简体   繁体   English

jQuery AJAX 调用控制器动作传递值数组

[英]jQuery AJAX call to Controller action passing array of values

I'm trying to pass an array of checkbox values from an Ajax form in MVC to a controller action.我正在尝试将一组复选框值从 MVC 中的 Ajax 表单传递给控制器​​操作。 Here's my code:这是我的代码:

The form (in a modal):形式(在模态中):

@using (Ajax.BeginForm("GetPrintableProject", "Project", null, new AjaxOptions { HttpMethod = "POST"}, new { @id = "PrintProjectFormId"))
 {
 <input type="hidden" name="projectId" id="projectId" value="">

} }

Some jQuery that gets an array of checkbox values:一些获取复选框值数组的 jQuery:

selectedProjects = projectsGrid.$('input[type="checkbox"]').serializeArray();

var projects = [];

$(selectedProjects).each(function (i, field) {
    projects.push(field.value);
});
//the hidden field in a modal I'm using
$('#projectId').val(projects.toString());

and the function I'm using to submit:以及我用来提交的功能:

$("#PrintProjectFormId").submit(function (event) {

event.preventDefault();
event.stopImmediatePropagation();

var action = $("#PrintProjectFormId").attr("action");
var dataString = new FormData($("#PrintProjectFormId").get(0));

$.ajax({
   type: "POST",
   url: action,
   data: dataString,
   dataType: "json",
   contentType: false,
   processData: false,
   success: function (result) {
            //stuff I'll get to later
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("There was a problem retrieving this project");
        }
        });
    });

and then the controller signature:然后是控制器签名:

[HttpPost]
public JsonResult GetPrintableProject(Guid[] projectId)

It works fine if I pass in a single value but if I pass in multiple values the controller just gets 'null'.如果我传入一个值,它工作正常,但如果我传入多个值,控制器只会得到“空”。 Think I'm missing something simple here.认为我在这里遗漏了一些简单的东西。 Thanks in advance.提前致谢。

I would suggest not changing default option values unless you need.除非您需要,否则我建议不要更改默认选项值。 Create you parameters as a JSON object and pass it to $.ajax .将参数创建为JSON对象并将其传递给$.ajax

From jQuery API for AJAX来自AJAX 的 jQuery API

processData (default: true) processData(默认值:true)

Type: Boolean类型:布尔型

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".默认情况下,作为对象传递给 data 选项的数据(从技术上讲,除了字符串之外的任何内容)将被处理并转换为查询字符串,适合默认的内容类型“application/x-www-form-urlencoded” . If you want to send a DOMDocument, or other non-processed data, set this option to false.如果要发送 DOMDocument 或其他未处理的数据,请将此选项设置为 false。

Setting this value to false will cause problems when attempting to pass more than one value to the action unless you do the processing within the controller function.除非您在控制器函数中进行处理,否则在尝试将多个值传递给操作时,将此值设置为 false 会导致问题。

 var action = $("#PrintProjectFormId").attr("action"); //fill projects array $.ajax({ type: "POST", url: action, data: { projectId: $("#PrintProjectFormId").val(), "projects": projects }, success: function(result) { //stuff I'll get to later }, error: function(jqXHR, textStatus, errorThrown) { alert("There was a problem retrieving this project"); } });

Please try this:请试试这个:

 type: "POST",
   url: action, 
   data: {dataString:dataString, dataString1:dataString2, dataString2:dataString2},

The format is: index:value格式为:索引:值

Since you are not passing class but array so your data string should be like:由于您不是传递类而是数组,因此您的数据字符串应如下所示:

   type: "POST",
   url: action, 
   data:
      [
        "6ccf7071-9b73-4e61-8736-58a842c131d0",
        "9e6c2748-18b5-4a53-a306-eb539c8b7dee",
        "5aa8901a-2c03-42d3-bf06-89f66e3797a4",
        "da5556bf-32aa-4f41-a64a-6e95fa2595c1"
      ],

Also try adding FromBody in ActionMethod like:还可以尝试在 ActionMethod 中添加FromBody ,例如:

[HttpPost]
public JsonResult GetPrintableProject([FromBody]Guid[] projectId)

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

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