1

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.

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

function

 $.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.

Controller

public ActionResult RealizarSimulacion(Array lista)
        {

            return View("Simulador");
        }

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

5
  • Related: You're receiving a list of objects because that's what you've declared that method to receive. Commented Dec 3, 2014 at 20:31
  • Yes, but i can't get the attributes of the objects, like administrador, fondoCartera, etc. Commented Dec 3, 2014 at 20:37
  • That's because object has no such properties. I've not actually done this myself (POSTing json like this), so I'm not sure exactly what the correct things would be to use, but I wonder if a list<dynamic> would work, or maybe there's another data structure.. Commented Dec 3, 2014 at 20:40
  • It doesn't work, the array comes like this: [1]"[object Object]" when you left the mouse over the variable. Commented Dec 3, 2014 at 21:48
  • What is the point of a multi-dimensional array here - the inner array only contains one object! It could be just list.push({ administrador: admin, fondoCartera: fondo, sec: secId, montoCartera: monto, porcentajeC: porcentaje});. Commented Dec 4, 2014 at 0:07

1 Answer 1

2

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; }
}

and adjust the script

$.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");
    });
  }
};

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
}

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help, i know about helpers, but i couldn't find a way to implement them for i wanted to do. I'm working in an MVC aplication, but the proyect is one of the 8 proyects(is an n-layer solution), so to acces a dto or structures is not easy, plus, i'm just learning MVC about a month, so, you are right, i have to study more MVC. Thanks for your help again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.