通过阵列通过AJAX MVC行动 [英] Pass array to mvc Action via AJAX

查看:76
本文介绍了通过阵列通过AJAX MVC行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从通过AJAX传递int数组(或IEnumerable的),以一个MVC的行动,我需要一点帮助。

在JavaScript是

  $得到('/控制器/ MyAction',{瓦尔斯:arrayOfValues​​},功能(数据){...

和控制器动作

 公众的ActionResult MyAction(IEnumerable的< INT> arrayOfValues​​)

在请求被格式化为当下

<$p$p><$c$c>controller/MyAction?_=1301503418429&arrayOfValues[]=491&arrayOfValues[]=368&arrayOfValues[]=235&arrayOfValues[]=437

所以我几乎没有,如果我走在方括号关我得到正确的响应。我应该如何传递数组到我得到这么控制器可以识别它是什么?

非常感谢您的帮助。

戴夫


解决方案

使得get调用之前设置了传统的属性设置为true。
即:

  jQuery.ajaxSettings.traditional = TRUE$获得('/控制器/ MyAction',{瓦尔斯:arrayOfValues​​},功能(数据){...

I'm trying to pass an array (or IEnumerable) of ints from via AJAX to an MVC action and I need a little help.

the javascript is

$.get('/controller/MyAction', { vals: arrayOfValues }, function (data) {...

and the controller action is

public ActionResult MyAction(IEnumerable<int> arrayOfValues )

At the moment the request is formatted as

controller/MyAction?_=1301503418429&arrayOfValues[]=491&arrayOfValues[]=368&arrayOfValues[]=235&arrayOfValues[]=437

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?

Many thanks for your help

Dave

解决方案

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行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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