Reputation: 115
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.
Upvotes: 1
Views: 2797
Reputation:
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.
Upvotes: 2