Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过AJAX将数组传递给mvc操作_C#_Asp.net Mvc_Jquery - Fatal编程技术网

C# 通过AJAX将数组传递给mvc操作

C# 通过AJAX将数组传递给mvc操作,c#,asp.net-mvc,jquery,C#,Asp.net Mvc,Jquery,我正试图通过AJAX将int数组(或IEnumerable)传递给MVC操作,我需要一些帮助 javascript是 $.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {... 控制器的动作是 public ActionResult MyAction(IEnumerable<int> arrayOfValues ) 我就快到了,如果我去掉方括号,我会得到正确的答案。我应该如何将该数组传递到

我正试图通过AJAX将int数组(或IEnumerable)传递给MVC操作,我需要一些帮助

javascript是

$.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {...
控制器的动作是

public ActionResult MyAction(IEnumerable<int> arrayOfValues )
我就快到了,如果我去掉方括号,我会得到正确的答案。我应该如何将该数组传递到get中,以便控制器能够识别它是什么

非常感谢你的帮助


Dave

你应该可以做得很好:

$.ajax({
   url: 'controller/myaction',
   data: JSON.stringify({
      myKey: myArray
   }),
   success: function(data) { /* Whatever */ }
});
那么你的行动方法是这样的:

public ActionResult(List<int> myKey)
{
    // Do Stuff
}
public ActionResult(列出我的密钥)
{
//做事
}

对你来说,看起来你只需要把你的价值观串起来。MVC中的JSONValueProvider将为您将其转换回IEnumerable

在进行get调用之前,将传统属性设置为true。 i、 e:


我过去在尝试执行POST时遇到过问题(不确定这是否正是您正在执行的操作,但我记得在传入数组时,传统必须设置为true

 var arrayOfValues = new Array();

 //Populate arrayOfValues 
 $.ajax({ 
      type: "POST",
      url: "<%= Url.Action("MyAction","Controller")%>",
      traditional: true,
      data: { 'arrayOfValues': arrayOfValues }              
 });
var arrayOfValues=new Array();
//填充ArrayOfValue
$.ajax({
类型:“POST”,
url:“”,
传统的:是的,
数据:{'arrayOfValues':arrayOfValues}
});

使用“传统”选项的答案是正确的。我只是为希望了解更多信息的人提供一些更多的背景信息

从jQuery文档中:

从jQuery 1.8开始,$.param()方法不再使用 jQuery.ajaxSettings.traditional作为其默认设置,并将 默认为false

您还可以在此处阅读更多内容: 和

HTH

相当晚了,但与这里已经给出的答案不同:

如果希望使用速记函数或,而不是
$.ajax
,则可以通过以下方式传递数组:


速记

var array = [1, 2, 3, 4, 5];
$.get('/controller/MyAction', $.param({ data: array }, true), function(data) {});

// Action Method
public void MyAction(List<int> data)
{
    // do stuff here
}
// Action Method
[HttpPost]
public void MyAction(List<int> data)
{
    // do stuff here
}

// Action Method
public void MyAction(List<int> data)
{
    // do stuff here
}
// Action Method
[HttpPost]
public void MyAction(List<int> data)
{
    // do stuff here
}
//动作方法
[HttpPost]
公共作废MyAction(列表数据)
{
//在这里做事
}

注:

  • 中的布尔参数用于 属性,哪个 必须为
    true
    ,此选项才能工作

您需要将数组转换为字符串:

//arrayOfValues = [1, 2, 3];  
$.get('/controller/MyAction', { arrayOfValues: "1, 2, 3" }, function (data) {...
它甚至以int、long或string的形式工作

public ActionResult MyAction(int[] arrayOfValues )
如果其他一切都失败了。。。 这里的其他答案都没有解决我的问题。我试图从JavaScript向MVC Web API控制器调用GET方法,并在该请求中发送一个整数数组作为参数。我在这里尝试了所有的解决方案,但控制器上的参数仍然为空(对于VB用户来说,没有)

我最终在a中找到了解决方案,实际上非常简单:只需在控制器中的数组参数之前添加
[FromUri]
注释(我还必须使用“传统”AJAX设置进行调用,以避免括号注释)。有关我在应用程序中使用的实际代码,请参见下文。


控制器签名: 注意:C#中的注释将是
[FromUri]


JavaScript:
实际URL字符串: 如果您使用的是ASP.NET Core MVC,并且需要处理方括号(而不是使用jQuery的“传统”选项),那么我找到的唯一选项就是在contoller方法中手动构建
IEnumerable

string arrayKey = "p[]=";
var pArray = HttpContext.Request.QueryString.Value
    .Split('&')
    .Where(s => s.Contains(arrayKey))
    .Select(s => s.Substring(arrayKey.Length));

如果您像我一样从.NET Framework MVC迁移到ASP.NET Core MVC,事情会有轻微的变化。ajax调用必须使用stringify so对原始对象进行,而不是传递
{vals:arrayOfValues}
的数据,它应该是
JSON.stringify(arrayOfValues)
,如下所示:

$.ajax({
   url: 'controller/myaction',
   data: JSON.stringify(arrayOfValues),
   success: function(data) { /* Whatever */ }
});
public ActionResult MyAction([FromBody] IEnumerable<int> arrayOfValues )
ASP.NET Core中所做的另一项更改是防止跨站点请求伪造,因此对于从ajax方法调用MVC操作,必须对操作参数应用
[FromBody]
atribute,如下所示:

$.ajax({
   url: 'controller/myaction',
   data: JSON.stringify(arrayOfValues),
   success: function(data) { /* Whatever */ }
});
public ActionResult MyAction([FromBody] IEnumerable<int> arrayOfValues )
公共操作结果MyAction([FromBody]IEnumerable ArrayOfValue)

这里有点晚了,但我可以使用SNag的解决方案进一步深入到$.ajax()。下面是代码,如果它能帮助任何人:

var array = [1, 2, 3, 4, 5];

$.ajax({
    type: "GET",
    url: '/controller/MyAction',
    data: $.param({ data: array}, true),
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
    },
    error: function (x, y, z) {
    }
});

// Action Method
public void MyAction(List<int> data)
{
    // do stuff here
}
var数组=[1,2,3,4,5];
$.ajax({
键入:“获取”,
url:“/controller/MyAction”,
数据:$.param({data:array},true),
contentType:'application/json;charset=utf-8',
成功:功能(数据){
},
错误:函数(x,y,z){
}
});
//作用方法
公共作废MyAction(列表数据)
{
//在这里做事
}

我使用的是asp.net core 2.2和jquery,必须使用简单的数据字段和一些数组将一个复杂的对象(“主类”)从视图提交给控制器。
一旦我在c#‘main class’定义中添加了数组(见下文)并通过ajax(post)提交了(正确填充的)数组,控制器中的整个对象就为空。
首先,我认为,我的ajax调用缺少“传统:真实”是原因,但事实并非如此。
在我的例子中,原因是c#“主类”中的定义。
在“主要课程”中,我有:

public List<EreignisTagNeu> oEreignistageNeu { get; set; }
我不得不将“主类”中的定义更改为:

 public List<int> oEreignistageNeu { get; set; }
public-List-oeeringistageneu{get;set;}
现在它工作了。
因此,对我来说,如果数组的列表未完全在“主类”中定义,则asp.net核心似乎有问题(与post有关)。
注:
在我的例子中,无论有没有“传统:正确”对于ajax调用

谢谢你的回答,但我认为我需要将“传统”设置为“真”。我认为这也可以。谢谢!你能概述一下传统ajax和非传统ajax之间的区别吗?我想更好地了解我对应用程序的其余部分所做的工作。你也可以添加“传统”将onal设置为单个调用。这样就不会影响REST。这个答案是正确的,但如果您更喜欢为单个调用设置“传统”,请检查@rionwillams answer。两个选项都进行表决。谢谢!@Tom Beech在jQuery 1.4中,$.param()方法递归序列化深度对象以适应现代脚本编写
public class EreignisTagNeu
{
 public int iHME_Key { get; set; } 
}
 public List<int> oEreignistageNeu { get; set; }