0

in my previos question Asp MVC 3 json complex object not initialize properties My mistake was in JSON convert from Knockout and after one more time with JSON.stringify(data).
Now evering working fine with one parameter, but I wonder about if I need send to MVC controller two or more parameters one of them is knowckout data = ko.toJSON(viewModel); variable other one is some text.

var settings = ko.toJSON(viewModel);

var parameters = JSON.stringify({ id : *"guid"*, data : settings });

                $.ajax({
                    url: '/KioskAjax/SaveSettings/',
                    type: "POST",
                    data: parameters,
                    dataType: "JSON",
                    contentType: "application/json; charset=UTF-8",
                    success: function (result) {
                        alert('ok');
                    }
                });    


 [HttpPost]
        public JsonResult SaveKiosksSettings(Guid id, GlobalData data)
        {
            return Json(false.ToString(), JsonRequestBehavior.AllowGet);
        }

In this example id is getting value, but GlobalData parameters is null again, i think this is because I use JSON.stringify again, but how build correct JSON for controller call if I have knowckout object ?

thanks.

Community
  • 1
  • 1
Arbejdsglæde
  • 13,670
  • 26
  • 78
  • 144

1 Answers1

3

ko.toJSON(myObject) does a ko.toJS(myObject) and then a JSON.stringify(myObject).

So, you could choose to use ko.toJS(myObject) to get a clean copy of your data and then JSON.stringify it with your other data, as you are already doing.

RP Niemeyer
  • 114,592
  • 18
  • 291
  • 211