Click here to Skip to main content
15,937,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends

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 as fallows:

var URL = "/DownloadExcel/Index?data="+self.qualities(); //self.qualities holds my entire list which hits break point in controller but i get Zero list ., "_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 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;
 , "_blank"); //on open of new window i get excel downloaded

My view :
<a data-bind="click:data.submit">download</a>

My view model Js:
self.submit = function()
var URL = "/DownloadExcel/Index?data="+self.qualities(); 
  , _blank); 

As mentioned above after click i get into submit function inside submit i have to do whatever i do to pass my observableArray to controller .

Any help is dearly appreciated .
Updated 5-Aug-14 8:29am

1 solution

There's probably a nicer way of doing it than this, but this is what I've put together in about 5 minutes. In this example, I'm posting in a list of Person objects.

public class Person
    public string Title { get; set; }

    public string Forename { get; set; }

    public string Surname { get; set; }

The controller class is as follows:

public class HomeController : Controller
    public ActionResult Index()
        return View();

    public ActionResult Index(string people)
        var serializer = new Newtonsoft.Json.JsonSerializer();
        var data = serializer.Deserialize<list><person>>(
            new JsonTextReader(
                new TextBufferReader(
                    new SeekableTextReader(people))));

        return View();

And finally the HTML and JavaScript:

<!DOCTYPE html>

        <form action="/Home/Index" method="POST">
            <input id="people" name="people" type="text"/>
            <button type="button" data-bind="click: submit">Submit</button>
    <script src="/Scripts/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/knockout-3.1.0.js" type="text/javascript"></script>
    <script type="text/javascript">
        var Person = function(title, forename, surname) {
            this.title = ko.observable(title);
            this.forename = ko.observable(forename);
            this.surname = ko.observable(surname);

        this.people = ko.observableArray();
        this.people.push(new Person("Mr", "Bob", "Marley"));
        this.people.push(new Person("Mrs", "Grace", "Jones"));
        this.people.push(new Person("Mr", "Mick", "Jagger"));

        function submit() {

Share this answer
sunil gutta 5-Aug-14 15:28pm    
Brent i can't thank you more for this help . i will learn something from this for sure. god bless mate . Thank you for your contribution .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900