0

As the title say, I need to send from a .js file some values to a MVC controller, one of that values is an object array that corresponds to an List<> in the controller, the problem is that when arrives to the controller the List count is 0, this is my code:

In the .js:

var listaParametros = [
               { "Identificador": "1", "Tipo": "MG", "Texto": "Escala de calificación", "Valor": "/EscalaCalificacion/Listado", "IdRetorno": identificadorRetorno, "RecuerdaFiltros": recuerdaFiltros }
    ];

    var maestroEscalaCalificacionE =
        {
            IdentificadorMaestroEscalaCalificacion: $('#grid-tableEscalaCalificacion').jqGrid('getRowData', elementoSeleccionado).IdentificadorMaestroEscalaCalificacion,
            IndicadorActivo: $('#ddlIndicadorActivo').val(),
            ListaParametros: listaParametros
        };

    $.redirectPost(window.rootUrl + "/EscalaCalificacion/Consultar", maestroEscalaCalificacionE);

The ActionResult on the Controller:

[HttpPost] 
public ActionResult Consultar(EscalaCalificacionMaestroE maestroEscalaCalificacionE)

The List is a public property of the class EscalaCalificacionMaestroE.

By the way, I'm using $.redirectPost() because I need to go to another page when the ActionResult finishes.

[UPDATE] This is the function redirectPost()

redirectPost: function (location, args) {
            var form = '';

            var jForm = $('<form></form>', {
                action: location,
                method: 'post'
            });

            $.each(args, function (key, value) {
                $("<input>", {
                    name: key,
                    value: value,
                    type: 'hidden'
                }).appendTo(jForm);
            });

            jForm.appendTo('body').submit();
        }

Now, when I use $.ajax, all works like a charm, but don't know how to get the new page, any help on getting to the new page?

  • What does `$.redirectPost` do ? never heard of it. Also you cannot (and should not try to ) pass a complex object via query string. You should do a form post. – Shyju Oct 03 '17 at 16:20
  • Updated the question! – Guillermo Alvarado Oct 03 '17 at 16:56
  • Well, if you already have an `$.ajax()` call that works, then all you have to do is calling `window.location.assign()` in your success function. Otherwise, if you want to do it by submitting a form, then just return a view in your action method and page will refresh. – derloopkat Oct 03 '17 at 17:06
  • Creating `input` elements won't work for `List` properties. Instead of that, [make an ajax post call](https://stackoverflow.com/a/4327046/3082296) and redirect to a new page in the ajax `success` callback. – adiga Oct 03 '17 at 18:30

1 Answers1

0

Thanks to everyone, your ideas help me to solve it, the architecture of the project, there are certain things that I can not use, but mixing ideas I solved it in the following way (I am aware that it is not the best in terms of security practices, but I can not change it):

I made a function similar to redirectPost () called "Redirection", but it would allow me to create an HTML input for each value in the list with the following format:

key: List [0] [PropertyName]

value: parameterValue

Then when the submit executes, the "List" on the controller has the right values.

Once again thanks everyone!

BTW, this is the code, maybe someone can need it!

function Redirection(location, args) {
    var form = '';
    var jForm = $('<form></form>', {
        action: location,
        method: 'post'
    });
    iterateValues(args, [], jForm, null, false)
    jForm.appendTo('body').submit();
}

function getInput(name, value, parent, isArray, isTraditionalArray) {
    var parentString;
    if (parent.length > 0) {
        parentString = parent[0];
        var i;
        for (i = 1; i < parent.length; i += 1) {
            parentString += "[" + parent[i] + "]";
        }
        if (isArray) {
            if (isTraditionalArray)
                name = parentString;
            else
                name = parentString + "[" + name + "]";
        } else {
            name = parentString + "[" + name + "]";
        }
    }
    return $("<input>").attr("type", "hidden")
        .attr("name", name)
        .attr("value", value);
};
function iterateValues(values, parent, form, isArray,  isTraditionalArray) {
    var i, iterateParent = [];
     Object.keys(values).forEach(function (i) {
        if (typeof values[i] === "object") {
            iterateParent = parent.slice();
            iterateParent.push(i);
            iterateValues(values[i], iterateParent, form, Array.isArray(values[i]), isTraditionalArray);
        } else {
             form.append(getInput(i, values[i], parent, isArray, isTraditionalArray));
        }
    });
};