抓住一个JSON数组到一个MVC3控制器动作 [英] Catch a JSON array into a MVC3 controller action

查看:96
本文介绍了抓住一个JSON数组到一个MVC3控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript数组是这样的:

  VAR optionArray =新的Array();
optionArray [totalBankAmount] = $(#totalBankAmountID)VAL()。
optionArray [singlePlayerAmount] = $(#singlePlayerAmountID)VAL()。
.....(更多阵列的数据)
 

我送这个数组像这样成StartOption(一MVC3控制器):

  $。阿贾克斯({
        网址:/ StartOption /首页/,
        数据:{allOptions:JSON.stringify(optionArray)},
        缓存:假的,
        键入:POST,
        超时:10000,
        数据类型:JSON,
        成功:函数(结果){
            ....
        }
});
 

如何索引操作将赶上optionArray?我是如何得到这些数据通过有数组索引的名字到我的索引行动?

请给我一个很好的例子,我相信新的MVC3。

和对不起我的英语不好。

解决方案
  

我有一个JavaScript数组是这样的:

没有,你没有一个数组。在JavaScript数组必须具有从零开始的整数索引。你所拥有的是一个JavaScript对象。

因此​​,有两种情景:

  • 您希望与对象一起工作
  • 您想要使用数组工作

让我们覆盖的对象的场景第一次。所以,你有一个对象:

 公共类Foo
{
    公共字符串吧{获得;组; }
    公共字符串巴兹{获得;组; }
}
 

,然后你想将它传递给控制器​​的动作:

  [HttpPost]
公众的ActionResult指数(富富)
{
    ...
}
 

下面是如何调用code可能是这样的:

 变种富= {};
富['酒吧'] ='一些吧;
富['巴兹'] ='一些巴兹';

//或者等价的:
//无功富= {};
// foo.bar ='些吧;
// foo.baz ='一些巴兹';

//或者等价的:
//无功富= {吧:一些吧,巴兹:一些巴兹'};

$阿贾克斯({
    网址:@ Url.Action(指数,startoption),
    的contentType:应用/ JSON的,
    数据:JSON.stringify({FOO:FOO}),
    键入:POST,
    超时:10000,
    成功:函数(结果){
        ....
    }
});
 


和第二种情况下,使用数组:

  [HttpPost]
公众的ActionResult指数(富[] FOOS)
{
    ...
}
 

和调用code:

  VAR FOOS = [];

变种foo1 = {};
foo1 ['酒吧'] ='一些吧1;
foo1 ['巴兹'] ='一些巴兹1';
foos.push(foo1);

变种foo2的= {};
foo2的['酒吧'] ='一些吧2';
foo2的['巴兹'] ='一些巴兹2';
foos.push(foo2的);

//或者等价的:
// VAR FOOS = [{吧:一些吧1,巴兹:一些巴兹1'},
// {吧:一些酒吧2',巴兹:一些巴兹2'}]。

$阿贾克斯({
    网址:@ Url.Action(指数,startoption),
    的contentType:应用/ JSON的,
    数据:JSON.stringify({FOOS:FOOS}),
    键入:POST,
    超时:10000,
    成功:函数(结果){
        ....
    }
});
 

I have an javascript array like this:

var optionArray = new Array();
optionArray["totalBankAmount"] = $("#totalBankAmountID").val();
optionArray["singlePlayerAmount"] = $("#singlePlayerAmountID").val();
..... (more array data)

I am sending this Array like this into StartOption (a MVC3 Controller):

$.ajax({
        url: "/StartOption/Index/",
        data: { allOptions: JSON.stringify(optionArray) },
        cache: false,
        type: "POST",
        timeout: 10000,
        dataType: "json",
        success: function (result) {
            ....
        }
});

How Index Action will catch that optionArray ? how i get those data by there array index name into my Index action ?

Please give me a good example, I am new in MVC3.

and sorry for my bad English.

解决方案

I have an javascript array like this:

No, you don't have an array. Arrays in javascript must have zero based integer indexes. What you have is a javascript object.

So there are 2 scenarios:

  • You want to work with objects
  • You want to work with arrays

Let's cover the objects scenario first. So you have an object:

public class Foo
{
    public string Bar { get; set; }
    public string Baz { get; set; }
}

and then you want to pass it to a controller action:

[HttpPost]
public ActionResult Index(Foo foo)
{
    ...
}

here's how the calling code might look like:

var foo = {};
foo['bar'] = 'some bar';
foo['baz'] = 'some baz';

// or the equivalent:
// var foo = {};
// foo.bar = 'some bar';
// foo.baz = 'some baz';

// or the equivalent:
// var foo = { bar: 'some bar', baz: 'some baz' };

$.ajax({
    url: '@Url.Action("index", "startoption")',
    contentType: 'application/json',
    data: JSON.stringify({ foo: foo }),
    type: 'POST',
    timeout: 10000,
    success: function (result) {
        ....
    }
});


And the second case, with arrays:

[HttpPost]
public ActionResult Index(Foo[] foos)
{
    ...
}

and the calling code:

var foos = [];

var foo1 = {};
foo1['bar'] = 'some bar 1';
foo1['baz'] = 'some baz 1';
foos.push(foo1);

var foo2 = {};
foo2['bar'] = 'some bar 2';
foo2['baz'] = 'some baz 2';
foos.push(foo2);

// or the equivalent:
// var foos = [ { bar: 'some bar 1', baz: 'some baz 1' }, 
//              { bar: 'some bar 2', baz: 'some baz 2' } ];

$.ajax({
    url: '@Url.Action("index", "startoption")',
    contentType: 'application/json',
    data: JSON.stringify({ foos: foos }),
    type: 'POST',
    timeout: 10000,
    success: function (result) {
        ....
    }
});

这篇关于抓住一个JSON数组到一个MVC3控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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