0

I want to send the viewmodel to my controller in mvc4. Everything I have tried comes back as the viewmodel is undefined. Everything else works fine. The page generates and shows all the values it should, but I can't seem to change the vm to json and send via post

view model

var guideviewmodel = function () {
var self = this;
self.scencustlist = ko.observableArray([]);
...}

ko.applyBindings(new guideviewmodel);

I have tried it both in the viewmodel and outside.

        $("#export").on("click", function (model, event) {
            var json = ko.toJSON(guideviewmodel);
            alert(json);
            $.ajax({
                type: 'POST',
                url: callPath + "/api/excel",
                cache: false,
                contentType: 'application/json; charset=utf-8',
                //data: JSON.stringify(self.scenppeditlist()),
                data: ko.toJSON(this.data),
                success: function () {
                    // success message 
                }
            });
        });

        self.exporttoexcel = function () {
            var json = ko.toJSON(guideviewmodel);
            alert(json);
            $.ajax({
                type: 'POST',
                url: callPath + "/api/excel",
                cache: false,
                contentType: 'application/json; charset=utf-8',
                //data: JSON.stringify(self.scenppeditlist()),
                //data: ko.toJSON(json),
                data: self,
                success: function () {
                    // success message 
                }
            });
        }
user1813251
  • 329
  • 3
  • 18

1 Answers1

0

If you try this it might work:

var vm = new guideviewmodel();
ko.applyBindings(vm);

$("#export").on("click", function (model, event) {
        var json = ko.toJSON(vm);
        alert(json);
        $.ajax({
            type: 'POST',
            url: callPath + "/api/excel",
            cache: false,
            contentType: 'application/json; charset=utf-8',
            //data: JSON.stringify(self.scenppeditlist()),
            data: ko.toJSON(this.data),
            success: function () {
                // success message 
            }
        });
    });
lagerone
  • 1,747
  • 13
  • 13