发送多维数组到控制器MVC 4 [英] Send multidimensional Array to controller MVC 4

查看:148
本文介绍了发送多维数组到控制器MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题发送多维数组我控制器。我有一个表有30行,此行是在for循环中创建的。

i have a problem sending a multidimensional array to my controller. I have a table with 30 rows, this rows are created in a for loop.

的事情是我创建一个多维数组我的表中的一行内的5个元素。

The thing is i create a multidimensional array for the 5 elements inside of one row of my table.

功能

 $.fn.RealizarCalculo = function (count) {

    var admin = $("#AdminSimple" + count).val();
    var fondo = $("#FondosSimple" + count + " option:selected").text();
    var secId = $("#FondosSimple" + count).val();
    var monto = $("#MontoSimple" + count).val();
    var porcentaje = $("#PorSimple" + count).val();
    var list = new Array();

    if (admin != "" && secId != "" && monto != "" && porcentaje != "") 
    {
        for (var i = 1; i <= 30; i++) 
        {
            var admin = $("#AdminSimple" + i).val();
            var fondo = $("#FondosSimple" + i + " option:selected").text();
            var secId = $("#FondosSimple" + i).val();
            var monto = $("#MontoSimple" + i).val();
            var porcentaje = $("#PorSimple" + i).val();

            list[i] = [{ administrador: admin, fondoCartera: fondo, sec: secId, montoCartera: monto, porcentajeC: porcentaje}];

        }

        $.ajax({
            url: "Simu/RealizarSimulacion",
            type: "POST",
            traditional: true,
            contentType: 'application/json',
            data: JSON.stringify({ lista:list }),
            dataType: 'json'
        }).done(function () {
            alert("sucess");
        });

    }
   };

我如何接受这阵我创造的?

How i receive this array i created?

我已经尝试过,但不工作,仅仅是接收对象的列表。

i have tried but is not working, is just receive a list of objects.

控制器

public ActionResult RealizarSimulacion(Array lista)
        {

            return View("Simulador");
        }

感谢您的帮助,对不起我的英语水平,我是智利。

Thanks for help, and sorry for my English, i'm chilean.

推荐答案

首先,你不(也不应该)将创建一个多维数组。对象的其阵列。其次,你不能回发到阵列(阵列是什么?与阵列是一个抽象类!)

Firstly, you do not (and should not) be creating a multi-dimensional array. Its an array of objects. Second you cant post back to Array (array of what? and Array is an abstract class!)

与您发布的属性创建一个对象(调整属性类型,以适应)

Create an object with the properties you are posting (adjust property types to suit)

public class MyClass
{
  public string administrador { get; set; }
  public string fondoCartera { get; set; }
  public string sec{ get; set; }
  public string montoCartera{ get; set; }
  public string porcentajeC { get; set; }
}

和调整脚本

$.fn.RealizarCalculo = function (count) {
  var admin = $("#AdminSimple" + count).val();
  ....
  var list = new Array();

  if (admin != "" && secId != "" && monto != "" && porcentaje != "") {
    for (var i = 1; i <= 30; i++) {
      var admin = $("#AdminSimple" + i).val();
      list.push({ administrador: admin, fondoCartera: fondo, sec: secId, montoCartera: monto, porcentajeC: porcentaje});
    }
    $.ajax({
      url: '@Url.Action("RealizarSimulacion", "Simu")', // dont hardcode url's!
      type: "POST",
      traditional: true,
      contentType: "application/json; charset=utf-8",
      data: JSON.stringify({ lista: list }),
      dataType: 'json'
    }).done(function () {
      alert("success");
    });
  }
};

和控制器方法(注意,您返回一个视图,但是你的AJAX功能预计JSON(数据类型:JSON)和你不这样做的东西返回重视反正!)

and the controller method (note you return a view, but you ajax function expects JSON (dataType: 'json') and you don't do anything with the returned value anyway!)

public ActionResult RealizarSimulacion(MyClass[] lista) // or List<MyClass> lista
{
  return View("Simulador"); // this makes no sense
}

这很难理解这是什么code是干什么的,但如果你真的有 ID = adminSimple1 ID控制= adminSimple2等需要停止编码,并开始学习MVC的一些基础知识,包括如何使用视图模型,以及如何使使用强类型HTML辅助视图中的控件。

It hard to understand what this code is doing, but if you really do have controls with id=adminSimple1", id=adminSimple2" etc. you need to stop coding and start learning some basics of MVC including how to use view models and how to render controls in a view using strongly typed html helpers.

这篇关于发送多维数组到控制器MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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