简体   繁体   English

如何将Ajax调用中的int数组从JS发送到控制器动作作为C#中的参数

[英]How to send int array from ajax call from JS to controller action as a parameter in C#

I have an object selectedDocTypes as [23,45,78] 我有一个对象selectedDocTypes为[23,45,78]

I need to pass it as a parameter to my controller action. 我需要将其作为参数传递给控制器​​操作。 But the value is not getting assigned to parameter, it is null. 但是该值未分配给参数,它为null。

Here is code: 这是代码:

JS: JS:

function addNewIssue(accountSID,selectedDocTypes, callback) {
var Data = { accountSID: accountSID, selectedDocTypes: selectedDocTypes };
$.ajax(landforceAPIURL + '/AddNewIssue', {
        cache: false,
        data: Data,
        success: function (data) {
             if (callback)
                    callback(data)
                },
        error: dataContext.queryFailed
        });
};

C#: C#:

[HttpGet, Route("AddNewIssue")]
public string AddNewIssue(string accountSID,int[] selectedDocTypes)
{...}

Assuming that you have correctly defined selectedDocTypes in JavaScript as an array, You have to specify contentType send to server in your ajax call, like: 假设您已在JavaScript中正确定义了selectedDocTypes作为数组,则必须在ajax调用中指定contentType发送到服务器,例如:

$.ajax(landforceAPIURL + '/AddNewIssue', {
        cache: false,
        data: Data,
        contentType: "application/json; charset=utf-8", //content type send to sever

I second what @haz commented. 我赞同@haz的评论。

However for this issue, this answer seems to have it covered: https://stackoverflow.com/a/13255459/4349196 . 但是,对于此问题,此答案似乎涵盖了: https : //stackoverflow.com/a/13255459/4349196

Another possible solution is to use custom JavaScript to construct a URL like: 另一种可能的解决方案是使用自定义JavaScript来构造URL,例如:

...accountSID=jSmith&selectedDocTypes=23&selectedDocTypes=45&selectedDocTypes=78

See here for a related example: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ . 有关相关示例,请参见此处: http : //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

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

相关问题 从视图中调用Controller中的Action并发送参数 - Call Action in Controller From View and send parameter Ajax无法使用带有2D数组参数的C#控制器操作 - Ajax Not Working With C# Controller Action With 2D Array Parameter 如何将int列表从Javascript(Ajax)发送到C#? - How to send a list of int from Javascript (Ajax) to C#? 如何从ajax调用控制器并发送$ _POST - How to call controller from ajax and send $_POST 我们如何使用JS文件中的AJAX调用来调用spring控制器(* .do操作)? - How can we call a spring controller (*.do action) using an AJAX call from a JS file? 如何将绑定到视图的模型从Ajax调用中单击到控制器动作? - How to send model binded to view from ajax call on button click to controller action? 将数组作为参数从JavaScript中的ajax调用传递给控制器 - Pass an array as parameter to a controller from an ajax call in JavaScript 从.js文件进行Ajax调用时,如何给控制器操作提供路径 - how do i give path to the controller action while ajax call from .js file 将带有香草 Js 的数组从 Ajax 发送到 Laravel controller - Send array with vanilla Js from Ajax to Laravel controller 从 JavaScript 到 C# Z594C103F2C6E04C3D8AB059F031E 中的不同文件夹进行 ajax 调用 - Make an ajax call from JavaScript to C# controller in a different folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM