-1

In my app i need to pass some object from view to another controller. Everything work fine for me but table which is part of this object is always null, even if it is populate proper in view. Let me show the code:

The view :

var loadNumber = @(Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model.LoadNumber)));
var data = @(Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model.Stops)));
var url = '@Url.Action("ReportViewer","LoadReport")';


var criteria = {LoadNumber: loadNumber,Stops: []};
for (var i = 0; i <= selected.length - 1; i++) {
     var id = selected[i].cells[1].innerText;
     for (var j = data.length - 1; j >= 0 ; j--) {
        if (data[j].StopmstSequence == id) {
             criteria.Stops.push(data[j].StopID);
             break;
        }
    }
}
window.open($.param.querystring(url, criteria));

In this part everything looks ok. Criteria object is populated well.

Controller :

public ActionResult ReportViewer(Criteria criteria)
{
    return View();
}

Of course this controler is much bigger but i can't show rest of code ( criteria object is most important )

Criteria Class :

public class Criteria
{
    public string LoadNumber { get; set; }
    public string[] Stops { get; set; }
} 

Now after run code in View the Criteria object in controller have LoadNumer set proper but Stops is always null. I try to change from string[] to List<string> or even IEnumerable<string> but always have same result. Can some one know why ?

Aht
  • 583
  • 4
  • 25
  • If you inspect `data` and `url` in the rendered html, are they proper values? – Travis J Mar 22 '16 at 19:37
  • Yes even in `window.open($.param.querystring(url, criteria));` all data are proper – Aht Mar 22 '16 at 19:38
  • What is your `param.querystring()` function? And what is its output? In order to bind to you method parameter, you would need `.../ReportViewer?LoadNumber=someValue&Stops=someValue&Stops=someValue` –  Mar 22 '16 at 22:25
  • And what is the point of your `var` statements and `for` loop (as opposed to just converting the `Model` to a javascript object? –  Mar 22 '16 at 22:28

1 Answers1

0

It's possible that the criteria json object is not URL encoded, so it's not getting completely sent. Since window.open is a GET operation, you would need to encode everything properly.

Seems similar to this: How to escape a JSON string to have it in a URL?

Community
  • 1
  • 1
Bryan Lewis
  • 5,629
  • 4
  • 39
  • 45