0

I'm processing some client data in a js function, and I need to pass the variable to a controller action. This is my function:

function Save() {
        var matrixIds = [];

        //Do something

        //Then create the URL and pass the parameter
        document.location = "ConciliacionItem/DetallesConciliacionManual/" + $.param({ matriz : matrixIds });

So far I've tried with several methods but I can't get to the Controller Action. The only thing that did work was to use an Ajax call like this one:

setTimeout(function () {
        $.ajax({
            type: "POST",
            url: myUrl,
            data: JSON.stringify({
                matriz: matrixIds
            }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            traditional: true
        });
    }, 500);

If i do this, I have the following problem: I need to process the data sent from this view, and the result goes to another view, like this:

public ActionResult DetallesConciliacionManual(int[][] matriz)
    {
        //Variable to process
        List<ConciliacionItem> listasAConciliar = new List<ConciliacionItem>();

        //Do a lot of things

        return View(listasAConciliar);
    }

So, what I need to do is to be able to pass the js variable of my function to this action, to continue with the app workflow. Is it possible in any way?

Thanks in advance.

2
  • $.ajax({ type: "POST", should it not be "Get" according to your question? Commented Mar 4, 2016 at 7:35
  • Maybe. Doesn't matter though, using ajax forces me to change everything in the Action, and still doing that I couldn't find a way to pass variables to the next view. Commented Mar 4, 2016 at 14:33

2 Answers 2

2

You can do it by manual creating of query string:

function Save() {
    var matrixIds = [];

    //Do something

    //Then create the URL and pass the parameter
    document.location = "ConciliacionItem/DetallesConciliacionManual?" + createQueryString('matriz', matrixIds);
}

function createQueryString(sendName, items)
{
    var result = '';
    for (var i = 0; i < items.length; i++) {
        for (var j = 0; j < items[i].length; j++) {
            result += sendName + '[' + i + '][' + j + ']=' + items[i][j] + '&';
        }
    }

    return result;
}

So you will get a string like

matriz[0][0]=0&matriz[0][1]=1&...

which will be successfully parsed into two dimensional int array for you action

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

1 Comment

Thanks a lot, mate. Last night I was able to solve this using this approach.
0

You may still use your ajax approach, then after processing your data, you may want to do:

public ActionResult DetallesConciliacionManual(int[][] matriz)
{
    //Variable to process
    List<ConciliacionItem> listasAConciliar = new List<ConciliacionItem>();

    //Do a lot of things

    //Returns another view.
    return RedirectToAction("Action", "Controller");
}

MSDN Reference

Comments

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.