通过 AJAX 将数组传递给 mvc Action [英] Pass array to mvc Action via AJAX
本文介绍了通过 AJAX 将数组传递给 mvc Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过 AJAX 将整数数组(或 IEnumerable)传递给 MVC 操作,我需要一些帮助.
I'm trying to pass an array (or IEnumerable) of ints from via AJAX to an MVC action and I need a little help.
javascript 是
the javascript is
$.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {...
控制器动作是
public ActionResult MyAction(IEnumerable<int> arrayOfValues )
此时请求的格式为
controller/MyAction?_=1301503418429&arrayOfValues[]=491&arrayOfValues[]=368&arrayOfValues[]=235&arrayOfValues[]=437
所以我快到了,如果我去掉方括号,我会得到正确的答案.我应该如何将该数组传递给我的 get 以便控制器可以识别它是什么?
So I'm almost there, if I take the square brackets off I get the correct response. How should I be passing that array into my get so that the controller can recognise what it is?
非常感谢您的帮助
戴夫
推荐答案
在调用 get 之前将传统属性设置为 true.即:
Set the traditional property to true before making the get call. i.e.:
jQuery.ajaxSettings.traditional = true
$.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {...
这篇关于通过 AJAX 将数组传递给 mvc Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文