super cool
super cool

Reputation: 6045

How to pass a ObservableArray to MVC controller ? Without Ajax

I am having an observable array with a list of view model data which i need to pass to Normal controller which is having a return type of ActionResult

      public ActionResult Index(list<qualities> data)
        {
          return excelCon.DownloadTMExcel(data); //here i get actionResult as ReturnType
        }

Java script code :

var URL = "/DownloadExcel/Index?data="+self.qualities(); //self.qualities holds my entire list which hits break point in controller but i get Zero list . 
        window.open(URL, "_blank"); 

Actually the excat scenario is when everything works fine i get excelsheet downloded with new window open .

I need advice in how to pass observableArray like the way i am dealing .

I tired also something like :

var URL = "/DownloadExcel/Index?data="+ko.toJson(self.qualities()); //this dont to controller itself 

I tried using Ajax call still it works one way i.e i can pass ObservableArray but ActionResult return Type it can't handle . always it goes to error function of ajax call and i wont get my excel downloaded .

The only case worked for me : There is other scenario i just need to pass parameters to controller then i am able to open a new window and download relevant excel .

  var URL = "/DownloadExcel/Index?typeId="+2; 
        window.open(URL, "_blank"); //on open of new window i get excel downloaded

In Additional :

 [HttpPost]
        public ActionResult Index(list<qualities> data) // i get count ZERO
        {
            return View();
        }

Using string Parameter

[HttpPost]
            public ActionResult Index(string data) //break point at controller not even hitting 
            {
                return View();
            }

Any help is dearly appreciated .

Upvotes: 0

Views: 2525

Answers (2)

sroes
sroes

Reputation: 15053

You need to encode the value:

var URL = "/DownloadExcel/Index?data=" + 
    encodeURIComponent(ko.toJson(self.qualities()));

If qualities contains too much data for the query string, you can use a post. For example:

<form method="post" action="/DownloadExcel/Index">
    <input type="hidden" name="data" data-bind="value: qualities" />
    <button>Download</button>
</form>

Upvotes: 1

MrYellow
MrYellow

Reputation: 409

GET requests have smaller limits than POST in the specs.

Originally GET was intended to be small and simple while POST was created with the vision that someday it could be used for file-uploads and the like. The limits you'll face with POST aren't from the spec but server configuration and technically it can be without limit.

On top of this restriction on GETs is browsers take great effort to cut down long URLs to avoid buffer-overflow exploits of the past where specially crafted URLs could execute arbitary code on the clients machine.

So your solution is more likely a POST request. One way of doing this aside from AJAX is saving the data into a hidden field:

<input type="hidden" id="json_to_send" name="json_to_send" />

document.getElementById('json_to_send').value = JSON.stringify(myobjectarray);

Then in PHP on the server-side:

$myobjectarray = json_decode(html_entity_decode($_POST['json_to_send']));

Upvotes: 1

Related Questions